2015-02-10 105 views
0

我有一個下拉菜單,可以篩選醫生專長並顯示選定專科的醫生。 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'), 

回答

1

下一步。 你的html只渲染一個只有頁碼的鏈接。嘗試:

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

這應該包括專業名稱。如果該作品也添加其他變量。

1

問題只是在你的下一個/上一個鏈接的hrefs中。您需要確保您在此添加語言/專業/性別參數以及頁碼。

有各種第三方模板過濾器可以幫助插入URL中的當前值。