我正在尝试将“表单”作为上下文传递,我的代码:
class BlogSearchView(ListView):
model = Blog
paginate_by = 20
template_name = "base/blog_search.html"
def get_queryset(self):
qset = super(BlogSearchView, self).get_queryset()
new_data = self.request.GET.copy()
form = BlogSearchForm(new_data)
if form.is_valid():
search_term = form.cleaned_data['q']#>> how to insert this in template
if search_term:
qset = qset.filter(Q(title__icontains=search_term)|
Q(body__icontains=search_term))
else:
qset = []
else:
form = BlogSearchForm() #>> how to insert this in template
return qset
尝试使用 get_context_data 但我收到错误。