Python 错误:";不能将“无”用作查询值;在Django中尝试使用ListView分页时
我想在搜索结果中实现分页。搜索后,我看到了良好的结果(例如) 但是,当我单击“下一步”时,我有一个错误: ValueError位于/search/,请求URL: 不能将“无”用作查询值 我认为URL(search_results.html)中存在这个问题。 我怎样才能修好它? 我怎样才能改变: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
<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传递?