首页 > 解决方案 > 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。

我觉得还有一些我需要添加的东西,但我不确定我还需要做什么才能让它工作。

标签: pythonhtmldjangosearch

解决方案


推荐阅读