Django为动态网址分页

时间:2015-02-10 14:24:50

标签: python django pagination

我有一个下拉菜单筛选医生专业化并向医生展示所选专业。文档列表是显示医生列表的视图。如果人们不从下拉菜单中选择任何内容,则只需访问此网址/doclistings/?selection=Choose+a+Speciality...&submit=并向所有医生展示。如果我点击第2页等/doclistings/?page=2

,分页也会有效

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

以下是显示医生名单的文件清单视图

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

以下是过滤医生的视图

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分页

<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 个答案:

答案 0 :(得分:1)

问题仅在于您的下一个/上一个链接的hrefs。您需要确保在那里添加语言/专业/性别参数以及页码。

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

答案 1 :(得分:1)

下一步。 您的html正在呈现仅包含页码的链接。尝试:

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

应该包括专业名称。如果可行则添加其他变量。