Question

I've been working with Django for a few months now so I'm still new at it.

I want to get the last GET parameter from the URL. Here is and example of the URL:

example.com?q=Something&filter1=Test&filter1=New&filter2=Web&filter3=Mine

Is there a way to get the last inserted GET parameter with django? It could be filter1, filter2 or filter3..

Maybe there is a way to do this after the initial refresh with javascript/jQuery?

Thanks!

Was it helpful?

Solution

You can try to parse url parameters yourself. For example:

Python/Django

from urlparse import urlparse, parse_qsl

full_url = ''.join([request.path, '?', request.META['QUERY_STRING']])
#example.com?q=Something&filter1=Test&filter1=New&filter2=Web&filter3=Mine
parameters = parse_qsl(urlparse(full_url)[4])
#[(u'q', u'Something'), (u'filter1', u'Test'), (u'filter1', u'New'), (u'filter2', u'Web'), (u'filter3', u'Mine')]
last_parameter = parameters[-1]
#(u'filter3', u'Mine')

Javascript

var params = window.location.search.split("&");
//["?q=Something", "filter1=Test", "filter1=New", "filter2=Web", "filter3=Mine"]
var last_param = params[params.length-1].replace("?","").split("=");
//["filter3", "Mine"]

This example do not use jQuery and provides basic knowledge of url parsing. There are a lot of libraries, that can do it for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top