问题描述
我正在尝试搜索cbv中的项目。尽管function based views
太简单了,但是我不知道如何在CBV中使用它。
我这样尝试过
views.py
class HomeView(ListView):
model = Item
#query = request.GET.get("q")
def get_context_data(self,**kwargs):
context = super(HomeView,self).get_context_data(**kwargs)
query = self.request.GET.get('q')
if query:
context['model'] = Item.objects.get(title=query)
print(query)
return context
paginate_by = 5
template_name = "home.html"
home.html
<form class="form-inline my-2 my-lg-0" method="GET" action="">
<input class="form-control mr-sm-2" type="text" name="q" placeholder="Search" value="{{ request.GET.q }}">
</form>
问题在于它显示了所有项目(不是专门搜索的项目)。
例:
在网址http://127.0.0.1:8000/?q=Blue
中,它不仅显示标题为Blue
的项目,而且还显示所有项目。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)