I'm just a beginner with Django, so you have to take my answer with a piece of salt.
First, request.path should return the path to the page without a domain, as per the documentation . Therefore, if the request is for http://example.com/ , request.path should just be "/" . Correct me if I am wrong.
But it does not matter. More importantly, request.path will not have any request parameters, so if the requested page is http://example.com/?PrevKey=PrevValue , then request.path will still be "/" . If you want to get request parameters, you must use dictionary-like access to the GET and POST (in this case GET) properties of the request object. Or better yet, access them through the QueryDict methods.
What I will do here, and this is far from the best method or code, is to prepare a custom template filter in which you pass the current request object and a key-value pair for testing.
Here's what your code template looks like. Note that you can still hardcode a pair of key values, although here it is formatted as a string with a "key colon value". A filter function can handle (if you need) more than one set of key-value pairs.
<a href="{{ request | addQueryParameter:'Key:Value'}}">My Link</a>
Filter function:
from urllib import urlencode def addQueryParameter(request, parameter): # parse the key-value pair(s) in parameter (which looks like a JSON string) # then add to dictionary d # note i am not taking into account white-space. for param in string.split(','): key, value = param.split(':', 1) d[key] = value # check if any keys in d are already in the request query parameters # if so, delete from d. If I misunderstood your question, then refactor # to delete from request.GET instead: for key in d.keys(): if key in request.GET.keys(): del d[key] # add the keys in request.GET into d: d.extend(request.GET.items()) # convert dictionary of key value pairs into a query string for urls, # prepend a ? queryString = "?%s" % urlencode(d) return "%s%s" % (request.path, queryString if queryString else "")
I must indicate that if the request for the page is http://example.com/login/?prev=news , and your template looks like
<a href="{{ request | addQueryParameter:'goto:dashboard,feature:products'}}">My Link</a>
Then the output will be (hopefully if it works):
<a href="/login/?prev=news&goto=dashboard&feature=products"> My Link</a>
Please note that there is no domain in this link (i.e. http://example.com/ ). This is because the filter does not add it. But you can change it to do this.
I will leave it to you to register this filter . More details here . Hope this helps.