错误:“无法使用None作为查询值”在Django中尝试用ListView进行分页时

时间:2019-09-11 06:35:33

标签: python django django-views

我想在搜索结果中实现分页。搜索后,我看到了很好的结果(例如http://127.0.0.1:8001/search/?q=mos

但是,当我单击“下一步” 时,出现错误:

  

/ search /处的ValueError,请求URL:http://127.0.0.1:8001/search/?city=2

     

不能将None用作查询值

我认为网址(search_results.html)中存在问题。 我该如何解决? 我该如何更改:

<a href="/search?city={{ page_obj.next_page_number }}">next</a>

models.py

from django.db import models

class City(models.Model):
    name = models.CharField(max_length=255)
    state = models.CharField(max_length=255)

    class Meta:
      verbose_name_plural = "cities"

    def __str__(self):
        return self.name

views.py

class HomePageView(ListView):
    model = City
    template_name = 'cities/home.html'
    paginate_by = 3
    page_kwarg = 'city'

def city_detail(request, pk):
    city = get_object_or_404(City, pk=pk)
    return render(request, 'cities/city_detail.html', {'city': city})


class SearchResultsView(ListView):
    model = City
    template_name = 'cities/search_results.html'
    paginate_by = 3
    page_kwarg = 'city'

    def get_queryset(self): # new
        query = self.request.GET.get('q')
        object_list = City.objects.filter(
            Q(name__icontains=query) | Q(state__icontains=query)
        )
        return object_list

urls.py

urlpatterns = [
    path('search/', SearchResultsView.as_view(), name='search_results'),
    path('', HomePageView.as_view(), name='home'),
    path('city/<int:pk>/', views.city_detail, name='city_detail'),
]

search_results.html

<ul>
  {% for city in object_list %}
    <li>
      {{ city.name }}, {{ city.state }}
    </li>
  {% endfor %}
</ul>

<div class="pagination">
    <span class="page-links">
        {% if page_obj.has_previous %}
            <a href="/search?city={{ page_obj.previous_page_number }}">previous</a>
        {% endif %}
            <span class="page-current">
                Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
            </span>
        {% if page_obj.has_next %}
            <a href="/search?city={{ page_obj.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>

home.html

<form action="{% url 'search_results' %}" method="get">
  <input name="q" type="text" placeholder="Search...">
</form>

<ul>
  {% for city in object_list %}
    <li>
      <h1><a href="{% url 'city_detail' pk=city.pk %}">{{ city.name }}</a></h1>
    </li>
  {% endfor %}
</ul>

2 个答案:

答案 0 :(得分:2)

您收到一条错误消息,指出 ValueError,查询为无,因为您没有在href中为您的qnext锚传递查询previous标签。

通过定义nextprevious锚标记来修改search_results.html,如下所示:

<a href="/search?city={{ page_obj.next_page_number }}&q={{ query }}">next</a>

现在打开views.py。在SearchResultsView内部,您需要向上下文字典中添加另一个名为query的键。为此,请定义一个名为 get_context_data(self, **kwargs)在SearchResultsView类中。

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['query'] = self.request.GET.get('q')
        return context

这样,query将被传递到模板中,以便您 next链接看起来像http://localhost:8000/search/?city=2&q=a

答案 1 :(得分:1)

修改 search_results.html

更改模板中的下一页和上一页链接,如下所示。

<a href="/search?q={{query}}&city={{ page_obj.previous_page_number }}">Previous</a>

<a href="/search?q={{query}}&city={{ page_obj.next_page_number }}">Next</a>

修改 urls.py

从搜索结果中删除“/”,如下所示...

path('search', SearchResultsView.as_view(), name='search_results'),

那么你的搜索 URL 将是这样的......

http://localhost:8000/search?q=abcd&city=2