我有一個下拉菜單,可以篩選醫生專長並顯示選定專科的醫生。 Doclisting是顯示醫生名單的觀點。如果人們沒有從下拉菜單中選擇任何東西,只需到/doclistings/?selection=Choose+a+Speciality...&submit=
這個URL並顯示所有的醫生。如果我點擊第2頁等/doclistings/?page=2
動態URL的Django分頁
當人們選擇專業化,例如牙醫/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))
下面的列表是過濾醫生
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'),
嘿謝謝你的答案。這只是照顧專業,但我也想考慮性別和語言。所以我嘗試了但給我無效塊標記:'endfor',預計'elif','else'或'endif'錯誤 – 2015-02-10 15:33:35
您錯過了{%endif%}標記。如果你需要一個。 – cdvv7788 2015-02-10 23:08:41