我想从part_list.html使用 name ='onePart'将 page_number 转移到网址。在我的代码 page_number = 1 中,但我想将其更改为paginator的当前页面取决于页面。我怎么能这样做?
P.S。抱歉我的英文:)
views.py :
def PartyNumView(request, page_number = 1):
all_parties = Part.objects.all()
current_page = Paginator(all_parties, 1)
try:
context = current_page.page(page_number)
except PageNotAnInteger:
context = current_page.page(1)
except EmptyPage:
context = current_page.page(current_page.num_pages)
return render_to_response('part_list.html', {'PartyNum': context})
def forOne(request, pk):
onePart = get_object_or_404(Part, pk=pk)
return render_to_response('SinglePart.html', {'onePart': onePart})
**urls.py:**
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^parties/(\d+)/$', PartyNumView),
url(r'^parties', PartyNumView),
url(r'parties/(?P<page_number>[\d]+)/(?P<pk>[\d]+)$', forOne, name='onePart'),
url(r'^main/', TemplateView.as_view(template_name='main.html')), #static html
url(r'^measures/', TemplateView.as_view(template_name='IcDesc.html')), #static html
]
一些HTML代码 part_list.html:
{% for object in PartyNum %}
<tr>
<td>{{ forloop.counter }}</td>
<td><a href="{% url 'onePart' pk=object.pk page_number=1%}"> {{ object.Party_number }}</a></td>
<td>{{ object.Film }}</td>
<td>{{ object.Thick }}</td>
<td>{{ object.Critical_temperature }}</td>
<td>{{ object.R_s }}</td>
{% endfor %}
</tbody>
</table>
</table>
<div class="row" style="margin:auto">
<div class="large-3 large-offset-5 columns">
<ul class="pagination">
{% if PartyNum.has_previous %}
<li class="arrow"><a href="/parties/{{ PartyNum.previous_page_number }}/">«</a></li>
{% else %}
<li class="arrow disabled"><a href="">«</a></li>
{% endif %}
{% for page in PartyNum.paginator.page_range %}
{% if page == PartyNum.number %}
<li class="current"><a href="/parties/{{ page }}/">{{ page }}</a></li>
{% else %}
<li><a href="/parties/{{ page }}/">{{ page }}</a></li>
{% endif %}
{% endfor %}
{% if PartyNum.has_next %}
<li class="arrow"><a href="/parties/{{ PartyNum.next_page_number }}/">»</a></li>
{% else %}
<li class="arrow disabled"><a href="">»</a></li>
{% endif %}
</ul>
</div>
</div>
我在django打招呼。如果你帮助我,我将不胜感激
答案 0 :(得分:0)
基于功能的视图分页
views.py
from django.contrib.auth.models import User
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
def index(request):
user_list = User.objects.all()
page = request.GET.get('page', 1)
paginator = Paginator(user_list, 10)
try:
users = paginator.page(page)
except PageNotAnInteger:
users = paginator.page(1)
except EmptyPage:
users = paginator.page(paginator.num_pages)
return render(request, 'core/user_list.html', { 'users': users })
user_list.html
<table class="table table-bordered">
<thead>
<tr>
<th>Username</th>
<th>First name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr>
<td>{{ user.username }}</td>
<td>{{ user.first_name }}</td>
<td>{{ user.email }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% if users.has_other_pages %}
<ul class="pagination">
{% if users.has_previous %}
<li><a href="?page={{ users.previous_page_number }}">«</a></li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
{% for i in users.paginator.page_range %}
{% if users.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if users.has_next %}
<li><a href="?page={{ users.next_page_number }}">»</a></li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
{% endif %}