I have a page that can be viewed by list or grid view through jQuery, but I want the URL to display what view it is — especially when user paginate so that they don’t have to reclick what they want to view — so something like this: /test/?grid_view or /test/?list_view.
I’ve been trying to use request.GET.get, but it doesn’t seem to be working. Here is what I have so far:
def test (request):
grid_view = request.GET.get('grid_view')
list_view = request.GET.get('list_view')
if grid_view:
return render_to_response('grid_view.html', {}, context_instance=RequestContext(request))
elif list_view:
return render_to_response('list_view.html', {}, context_instance=RequestContext(request))
else:
return render_to_response('default_view.html', {}, context_instance=RequestContext(request))
Then in my main template I’d point the different views to <a href="/?list_view/">list view</a>, etc. There is probably a better way to do it so any suggestions are appreciated too.
An empty string is evaluated to False and, that is what your GET request Query String parameters will contain.
You could modify your code to:
Or: