python - Django NameError:请求未在基于类的视图中定义
问题描述
我正在尝试将搜索功能添加到我的网站中。但我似乎找不到使用基于类的视图的视频。
意见:
class IndexView(ListView):
model = Post
# queryset = Post.objects.filter(live=True)
template_name = "public/index.html"
def get_queryset(self):
query = request.GET.get("q")
if query:
queryset_list = queryset_list.filter(title_icontains=query)
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['queryset'] = Post.objects.filter(live=True)
context['category'] = Category.objects.all()
return context
HTML:
<form method='GET' action=''>
<p class="text-muted">Keywords:</p>
<input type="text" name='q' class="homebanner-search" placeholder="Enter your keywords">
<input type="submit" value="search">
</form>
当我尝试运行服务器时,我得到一个 'NameError: name 'request' is not defined。
我觉得还有一些我需要添加的东西,但我不确定我还需要做什么才能让它工作。
解决方案
推荐阅读
- python-2.7 - os.path.isdir() 总是给真
- java - 记忆游戏java gui
- c++ - 如何从原始指针中获取 shared_ptr?
- javascript - 上传图片,无法读取未定义的属性“订阅”
- laravel - laravel 5 show 正在返回空查询并且不显示任何数据
- java - java DateTimeFormatterBuilder 在测试时间失败
- regex - 如何一次 grep 两种模式并将结果放在一个字符串中?
- java - 选择特定工作区时,Eclipse 在后台运行
- r - 使用 r 在同一图表上绘制月度和年度数据
- spring - 春休服务。无效的块头