Reputation: 1153
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. 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. The pagination also works if I click on page 2 and so on /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. 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
<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'),
Upvotes: 0
Views: 587
Reputation: 2089
Next. Your html is rendering a link with only a page number. 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.
Upvotes: 1
Reputation: 599450
The problem is simply in the hrefs for your next/previous links. 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.
Upvotes: 1