python 上下文必须是字典而不是模块

js81xvg6  于 2022-10-30  发布在  Python
关注(0)|答案(1)|浏览(146)

当我过滤我的搜索栏时,我得到这个错误。我不确定我在这里做错了什么
观看本教程:https://www.youtube.com/watch?v=llbtoQTt4qw&t=3399s

查看次数.py

class pplList(LoginRequiredMixin,ListView):
        model = People
        context_object_name = 'people'
        def get_context_data(self,**kwargs):
            search_input = self.get.GET.get('search-area') or ''
            if search_input:
                context['people'] = context['people'].filter(name__icontains=search_input)
            return context

人员列表.html

{%if request.user.is_authenticated %}
    <p>{{request.user}}</p>
    <a href="{% url 'logout' %}">Logout</a>
{% else %}
    <a href="{% url 'login' %}">Login</a>
{% endif %}

<hr>
<h1>Interviewee Dashboard {{color}}</h1>

<a href="{% url 'pplCre' %}"> Add Candidates</a>
<form method="get">
    <input type = 'text' name = 'search-are'>
    <input type = 'submit' value = 'Search'>

</form>
<table>
    <tr>
        <th> Item</th>
        <th> </th>

    </tr>
    {% for people in people %}
    <tr>
        <td>{{people.name}}</td>
        <td><a href="{% url 'pplDet' people.id %}">View</a></td>
        <td><a href="{% url 'pplUpd' people.id %}">Edit</a></td>
        <td><a href="{% url 'pplDel' people.id %}">Delete</a></td>
    </tr>
    {% empty %}
    <h3>No items in list</h3>
    {% endfor %}
</table>
ffx8fchx

ffx8fchx1#

有一些小错误,比如它应该是self.request.GET.get('search-area'),而且你还没有调用super()方法,所以试试这个视图:

class pplList(LoginRequiredMixin,ListView):
    model = People
    context_object_name = 'people'
    def get_context_data(self,**kwargs):
        context=super().get_context_data(**kwargs)
        search_input = self.request.GET.get('search-area', False)
        if search_input:  
           context['people']= People.objects.filter(name__icontains=search_input)
        return context

此外,基于类的视图通常用PascalCase编写,因为它们是Python的类,需要将它们的名称写为模型名称作为前缀,实际视图名称作为后缀,因此应该是PeopleListView

相关问题