Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 错误:";不能将“无”用作查询值;在Django中尝试使用ListView分页时_Python_Django_Django Views - Fatal编程技术网

Python 错误:";不能将“无”用作查询值;在Django中尝试使用ListView分页时

Python 错误:";不能将“无”用作查询值;在Django中尝试使用ListView分页时,python,django,django-views,Python,Django,Django Views,我想在搜索结果中实现分页。搜索后,我看到了良好的结果(例如) 但是,当我单击“下一步”时,我有一个错误: ValueError位于/search/,请求URL: 不能将“无”用作查询值 我认为URL(search_results.html)中存在这个问题。 我怎样才能修好它? 我怎样才能改变: <a href="/search?city={{ page_obj.next_page_number }}">next</a> 视图.py from djan

我想在搜索结果中实现分页。搜索后,我看到了良好的结果(例如)

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

ValueError位于/search/,请求URL:

不能将“无”用作查询值

我认为URL(search_results.html)中存在这个问题。 我怎样才能修好它? 我怎样才能改变:

<a href="/search?city={{ page_obj.next_page_number }}">next</a>
视图.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
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
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'),
]
url.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
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
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'),
]
urlpatterns=[
路径('search/',SearchResultsView.as_view(),name='search_results'),
路径(“”,HomePageView.as_view(),name='home'),
路径('city/',views.city\u detail,name='city\u detail'),
]
搜索结果.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>
<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>
    {对象列表%中城市的百分比}
  • {{city.name},{{city.state}
  • {%endfor%}
{%如果页面_obj.has_previous%} {%endif%} 第{{Page_obj.paginator.num_pages}页中的第{{Page_obj.number}页。 {%如果页面_obj.has_next%} {%endif%}
home.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>
<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>

    {对象列表%中城市的百分比}
  • {%endfor%}

您会收到一个错误,提示ValueError,query为None,因为您没有在href中为您的
下一个
上一个
锚定标记传递查询
q

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

<a href="/search?city={{ page_obj.next_page_number }}&q={{ query }}">next</a>
这样,
query
将被传递到模板中,以便
next
链接看起来像
http://localhost:8000/search/?city=2&q=a

修改搜索结果.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>
<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>
更改模板中的下一页和上一页链接,如下所示


修改URL.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
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
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'),
]
从搜索结果中删除“/”,如下所示

path('search',SearchResultsView.as_view(),name='search_results'),
然后你的搜索URL将是这样的


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

您是否可能试图在搜索视图中获取查询参数
q
,但在调用
a href
中的端点时,实际使用
city
query\u参数调用端点。当你试图获得
q
时,你一个也得不到。我不明白,你为什么要把页码作为城市的pk传递?