动态URL的Django分页

2021-01-17 13:52:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个下拉列表来过滤医生专业,并显示所选专业的医生。Doclisting是显示医生列表的视图。如果人们没有从下拉列表中选择任何内容,它只会转到这个url /doclistings/?selection=Choose+a+Speciality...&submit=并显示所有医生。如果我单击第2页,依此类推,分页也有效/doclistings/?page=2

当人们选择一个专业,例如牙医/doclistings/?speciality=Dentist&gender=Select+a+Gender&language=Choose+a+Language,然后点击分页页面2,它就会显示没有牙医的通用/doclistings/?page=2。不保存分页时单击了专门化。在

这里是doclisting视图,显示医生列表

def doclistings(request):
    d = getVariables(request)
    if request.method == "GET":
        form = DropdownSelectionForm(request.GET)
        try:
            s_name = request.GET['speciality']
        except:
            s_name = None
        try:
            l_name = request.GET['language']
        except:
            l_name = None
        try:
            g_name = request.GET['gender']
        except:
            g_name = None

        d['s_name'] = s_name 
        d['l_name'] = l_name
        d['g_name'] = g_name


        try:
            doctors = filter_doctors(request=request, specialization=s_name, gender=g_name, language=l_name).order_by('-netlikes')
            paginator = Paginator(doctors, 15) # Show 15 doctors per page
            page = request.GET.get('page')

        except Exception:
            return error404(request)


        if doctors == None: 
            return error404(request)

        if len(doctors) == 0:
            d['not_found'] = "anything you want here :)"

        try:
            doctors = paginator.page(page)

        except PageNotAnInteger:
            doctors = paginator.page(1)

        except EmptyPage:
            doctors = paginator.page(paginator.num_pages)

    else:
        form = DropdownSelectionForm()

    d['doctors'] = doctors
    d.update({'form': form, 'languages': Language.objects.all()})
    return render_to_response('m1/doclistings.html',d, context_instance=RequestContext(request))

这是过滤医生的观点

^{pr2}$

在文档列表.html分页

<ul class="pagination nav navbar-nav">
    {% if doctors.has_previous %}
            <li><a href="?page={{ doctors.previous_page_number }}">Prev</a></li>
    {% endif %}
    {% for page in doctors.paginator.page_range %}
        <li class="{% if doctors.number == page  %}active{% endif %}"><a href="?page={{page }}">{{ page }}</a></li>
    {% endfor %}

    {% if doctors.has_next %}
        <li> <a href="?page={{ doctors.next_page_number }}">Next</a></li>
    {% endif %}
 </ul>

网址

url(r'^doclistings/$', views.doclistings, name='doclistings'),
2条回答
网友
1楼 ·

下一个。 你的html正在呈现一个只有页码的链接。尝试:

<a href="?page={{ doctors.next_page_number }}{% if s_name %}&speciality={{ sname }}{% endif %}">

应该包括专业名称。如果可以的话,还可以添加其他变量。在

网友
2楼 ·

问题只是在下一个/上一个链接的href中。您需要确保添加语言/专业/性别参数以及页码。在

有各种第三方模板过滤器,可以通过从URL中插入当前值来提供帮助。在

相关问题