动态 url 的 Django 分页
Django pagination for dynamic urls
我有一个下拉菜单可以过滤医生专业并显示 selected 专业的医生。 Doclisting 是显示医生列表的视图。如果人们没有从下拉列表中 select 任何内容,它只会转到此 url /doclistings/?selection=Choose+a+Speciality...&submit=
并显示所有医生。如果我点击第 2 页等 /doclistings/?page=2
,分页也有效
当人们 select 一个专业,例如牙医 /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>
urls
url(r'^doclistings/$', views.doclistings, name='doclistings'),
问题出在您的 next/previous 链接的 href 中。您需要确保在那里添加 language/speciality/gender 参数以及页码。
有各种第三方模板过滤器可以帮助插入 URL 中的当前值。
接下来。
您的 html 正在呈现只有页码的 link。尝试:
<a href="?page={{ doctors.next_page_number }}{% if s_name %}&speciality={{ sname }}{% endif %}">
那应该包括专业名称。如果可行,也添加其他变量。
我有一个下拉菜单可以过滤医生专业并显示 selected 专业的医生。 Doclisting 是显示医生列表的视图。如果人们没有从下拉列表中 select 任何内容,它只会转到此 url /doclistings/?selection=Choose+a+Speciality...&submit=
并显示所有医生。如果我点击第 2 页等 /doclistings/?page=2
当人们 select 一个专业,例如牙医 /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>
urls
url(r'^doclistings/$', views.doclistings, name='doclistings'),
问题出在您的 next/previous 链接的 href 中。您需要确保在那里添加 language/speciality/gender 参数以及页码。
有各种第三方模板过滤器可以帮助插入 URL 中的当前值。
接下来。 您的 html 正在呈现只有页码的 link。尝试:
<a href="?page={{ doctors.next_page_number }}{% if s_name %}&speciality={{ sname }}{% endif %}">
那应该包括专业名称。如果可行,也添加其他变量。