Python 使用ListView django分页
我想创建具有搜索和分页功能的应用程序。分页不适用于ListView 当我点击“下一步”链接时,我正在从起始页-->移动到列表中,但列表中的元素没有改变 下一步单击“下一步”链接不会更改url(->) 你能帮我找出错误吗? 我认为*.html文件中存在该错误,但找不到它 我的代码: 型号.pyPython 使用ListView django分页,python,django,python-3.x,Python,Django,Python 3.x,我想创建具有搜索和分页功能的应用程序。分页不适用于ListView 当我点击“下一步”链接时,我正在从起始页-->移动到列表中,但列表中的元素没有改变 下一步单击“下一步”链接不会更改url(->) 你能帮我找出错误吗? 我认为*.html文件中存在该错误,但找不到它 我的代码: 型号.py from django.db import models class City(models.Model): name = models.CharField(max_length=255)
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
# cities/urls.py
from django.urls import path
from . import views
from .views import HomePageView, SearchResultsView
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'),
]
from django.shortcuts import render
from django.views.generic import TemplateView, ListView
from .models import City
from django.db.models import Q
from django.shortcuts import render, get_object_or_404
class HomePageView(ListView):
model = City
template_name = 'cities/home.html'
paginate_by = 3
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'
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
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
# cities/urls.py
from django.urls import path
from . import views
from .views import HomePageView, SearchResultsView
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'),
]
from django.shortcuts import render
from django.views.generic import TemplateView, ListView
from .models import City
from django.db.models import Q
from django.shortcuts import render, get_object_or_404
class HomePageView(ListView):
model = City
template_name = 'cities/home.html'
paginate_by = 3
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'
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
home.html
<!-- templates/home.html -->
<h1>HomePage</h1>
<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>
{{page_obj}}
<div class="pagination">
<span class="page-links">
{% if page_obj.has_previous %}
<a href="/?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="/?city={{ page_obj.next_page_number }}">next</a>
{% endif %}
</span>
</div>
主页
{对象列表%中城市的百分比}
-
{%endfor%}
{{page_obj}}
{%如果页面_obj.has_previous%}
{%endif%}
第{{Page_obj.paginator.num_pages}页中的第{{Page_obj.number}页。
{%如果页面_obj.has_next%}
{%endif%}
页面查询参数的标准名称是'page'
您应该更改查询参数的名称,或者使用?page=
参数呈现模板
选项1:更改页面
您可以通过更改以下选项来更改:
页面查询参数的标准名称是'page'
,您应该更改queryparameter的名称,或者使用?page=
参数呈现模板
选项1:更改页面
您可以通过更改以下选项来更改: