简体   繁体   English

Django分页用于动态网址

[英]Django pagination for dynamic urls

I've a dropdown to filter doctor specializations and show the doctors for the selected specialization. 我有一个下拉菜单可以过滤医生的专业知识,并显示所选专业的医生。 Doclisting is the view that shows the list of the doctors. Doclisting是显示医生列表的视图。 If people don't select anything from the dropdown, it just goes to this url /doclistings/?selection=Choose+a+Speciality...&submit= and show all the doctors. 如果人们没有从下拉菜单中选择任何内容,它将直接转到该网址/doclistings/?selection=Choose+a+Speciality...&submit=并显示所有医生。 The pagination also works if I click on page 2 and so on /doclistings/?page=2 如果我单击第2页,等等, /doclistings/?page=2则分页也有效

The problem comes when people select a specialization, for instance Dentist /doclistings/?speciality=Dentist&gender=Select+a+Gender&language=Choose+a+Language and then click on the pagination page 2, it then just shows them the generic /doclistings/?page=2 without the dentist. 问题出在人们选择专业时,例如Dentist /doclistings/?speciality=Dentist&gender=Select+a+Gender&language=Choose+a+Language然后单击分页2,然后仅向他们显示通用的/doclistings/?page=2没有牙医。 It's not saving the specialization when the pagination is clicked. 单击分页时并没有保存专业化。

Here is the doclisting view to show the list of doctors 这是显示医生列表的文档列表视图

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))

Here is the view that filters the doctors 这是过滤医生的视图

def filter_doctors(request=None, specialization=None, language=None, gender=None):
    query = Doctor.objects.filter()

    if specialization and specialization != "All Doctors":
        try:
            spec = Specialization.objects.get(name = specialization) # assuming that no errors here
            query = query.filter(specialization=spec)
        except:
            return None
    if language and language != "Choose a Language":
        try:
            lang = Language.objects.get(name=language)
            query = query.filter(language=lang)
        except:
            return None
    if gender and gender != "Select a Gender":
        if gender != "Male" and gender != "Female":
            return None
        query = query.filter(gender=gender)
    return query

doclisting.html pagination doclisting.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>

urls 网址

url(r'^doclistings/$', views.doclistings, name='doclistings'),

The problem is simply in the hrefs for your next/previous links. 问题出在下一个/上一个链接的hrefs中。 You need to ensure you add the language/speciality/gender parameters there as well as the page number. 您需要确保在其中添加语言/专业/性别参数以及页码。

There are various third-party template filters that will help by inserting the current values from the URL. 有各种各样的第三方模板过滤器,它们可以通过插入URL中的当前值来提供帮助。

Next. 下一个。 Your html is rendering a link with only a page number. 您的html呈现的链接只有页码。 try: 尝试:

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

That should include the specialty name. 那应该包括专业名称。 If that works add the other variables too. 如果可行,也添加其他变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM