我无法将Django视图与Django模板连接起来。我试图使用FormMixin和ListView基于类的视图。
class Merchants(FormMixin, ListView):
"""
A view of that shows a list of all the merchants.
"""
template_name = "reporting/merchants.html"
model = models.Merchant
context_object_name = "merchants"
def get_queryset(self):
queryset = super(ViewClassName, self).get_queryset()
search_query = self.request.GET.get("q", None)
if search_query:
queryset = queryset.filter(name__ilike=search_query)
return queryset
我的目标是在我的模板中使用我的get_queryset函数,以允许用户搜索商家。这个表单发布到自己,但不幸的是它没有返回任何东西。我在FormMixins上阅读过Django文档,但仍然无法弄明白。任何帮助都必须得到赞赏。
<form action= "">
<input name="q" placeholder="search for merchant">
<button type="submit">Search </button>
</form>
谢谢!
答案 0 :(得分:0)
您有缩进问题:
if search_query:
queryset = queryset.filter(name__ilike=search_query)
return queryset # <--- this should be outside of if statement
应该是:
if search_query:
queryset = queryset.filter(name__ilike=search_query)
return queryset