Python 使用post表单更新Django ListView
上下文 我想使用POST表单更新我的Python 使用post表单更新Django ListView,python,django,Python,Django,上下文 我想使用POST表单更新我的CardListView的查询集。我已经看过了其他描述如何使用GET的帖子,尽管我想使用POST实现它。我的当前代码根据用户在表单中输入的内容成功更新视图。但是,我在分页和URL方面遇到了问题 问题 class CardSearchForm(forms.ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs)
CardListView
的查询集。我已经看过了其他描述如何使用GET的帖子,尽管我想使用POST实现它。我的当前代码根据用户在表单中输入的内容成功更新视图。但是,我在分页和URL方面遇到了问题
问题
class CardSearchForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field in self.Meta.not_required:
self.fields[field].required = False
class Meta:
model = Card
fields = ['card_id', 'owner_first_name', 'owner_last_name']
not_required = ['card_id', 'owner_first_name', 'owner_last_name']
如果我已经导航到列表视图中的另一个页面,比如第2页,我的URL将变成:http://localhost:8000/cards/?page=2
。然后,如果我在此页面上提交帖子表单,我的URL不会更新为http://localhost:8000/cards
(即它保持为http://localhost:8000/cards/?page=2
)因此,如果搜索表单的结果是第2页没有结果,则会出现“未找到页面”(404)错误
我意识到这是因为我在
post()
方法中返回的方式。我使用的是render()
而不是redirect()
,但是我似乎无法让表单使用redirect()
我已经尝试在我的post()
方法中对paginator page对象进行硬编码,尽管这不会更改URL“?page=2”,因此没有帮助
以下是相关代码: CardListView
class CardListView(LoginRequiredMixin, ListView):
model = Card
template_name = 'cards/card_list.html'
ordering = ['card_id']
paginate_by = 2
def post(self, request, *args, **kwargs):
""" Executed upon submission of a POST form. """
# Set the object list to the filtered query set
self.object_list = self.get_queryset()
# Get the context of the class
self.context = self.get_context_data()
# Render updated view with the class's context and filtered queryset
return render(request, self.template_name, self.context)
def get_context_data(self, **kwargs):
context = super(CardListView, self).get_context_data(**kwargs)
# Add search form to the context of the view so that it can be accessed within the template
if 'card_search_form' not in context:
context['card_search_form'] = CardSearchForm()
return context
def get_queryset(self):
# Get the user input data from the search form
search_form = CardSearchForm(self.request.POST)
# Check if request is a POST request, and if the search form is valid
if self.request.method == "POST" and search_form.is_valid():
search_card_id = search_form.cleaned_data['card_id']
search_owner_first_name = search_form.cleaned_data['owner_first_name']
search_owner_last_name = search_form.cleaned_data['owner_last_name']
return self.send_filtered_results(
search_card_id,
search_owner_first_name,
search_owner_last_name
)
# Either the request was a GET request (e.g. from when first navigating to the page) or invalid form
else:
# Return all database entries (ordered)
return self.model.objects.order_by('card_id')
def send_filtered_results(self, search_card_id, search_owner_first_name, search_owner_last_name):
# Filter the objects based upon the search form
return self.model.objects.filter(
card_id__icontains=search_card_id,
owner_first_name__icontains=search_owner_first_name,
owner_last_name__icontains=search_owner_last_name
).order_by('card_id')
卡片搜索表单
class CardSearchForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field in self.Meta.not_required:
self.fields[field].required = False
class Meta:
model = Card
fields = ['card_id', 'owner_first_name', 'owner_last_name']
not_required = ['card_id', 'owner_first_name', 'owner_last_name']