Link to current page in Django, with optional GET Params?

How do I place an HTML link related to the current page and that adds extra GET parameters (or overwrites existing ones if they already exist)?

Now I have something like:

<a href="{{ request.path }}?Key=Value"> My Link </a> 

Currently, request is being sent to the page. If request.path /qaru.site / ... , then the resulting link will be /qaru.site / ...

Problem?

But of course, if the current URL is /qaru.site / ... , then it will look like this:

/qaru.site / ...

Pay attention to the wrong second question mark - in fact it should be:

/qaru.site / ...

In addition, if there is already a key with the same name, then instead of overwriting it, my current solution ignores it - this is incorrect.

How to solve these two problems?

+6
source share
4 answers

You will need a special tag. There are a couple at djangosnippets - this one looks pretty comprehensive.

+4
source

For anyone who sees this in the future: https://bitbucket.org/monwara/django-url-tools

+1
source

You can use: {{request.get_full_path}} to get the current path with additional parameters.

0
source

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> <!-- the order of the above parameters may differ --> 

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.

0
source

Source: https://habr.com/ru/post/890943/


All Articles