Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django template tag to insert or replace URL parameter

Is anyone aware of a Django template tag that takes the current path and query string and inserts or replaces a query string value?

e.g. given a request to /some/custom/path?q=how+now+brown+cow&page=3&filter=person

The call {% urlparam 'page' 4 %} would generate /some/custom/path?q=how+now+brown+cow&page=4&filter=person.

This wouldn't be too difficult to write from scratch, but as this seems like a very common task, I would expect a tag like this to be built-in. However, after reading through the docs and googling, I can't seem to find anyone's who's publicized such a tag.

like image 298
Cerin Avatar asked May 09 '13 19:05

Cerin


People also ask

How do I pass URL parameters in Django?

Django URL pass parameter to view You can pass a URL parameter from the URL to a view using a path converter. Then “products” will be the URL endpoint. A path converter defines which type of data will a parameter store. You can compare path converters with data types.

What is the use of template tag in Django?

The template tags are a way of telling Django that here comes something else than plain HTML. The template tags allows us to to do some programming on the server before sending HTML to the client.

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.


1 Answers

Since I haven't used these tools by my own, I'll just refer you:

  • django-url-tools has url_params template tag that seems to do what you want
  • django-more-template-tags-and-filters has some helper tags for manipulating urls and parameters

FYI, I've personally used jsurl library for this kind of url manipulations in javascript.

Hope that helps.

like image 180
alecxe Avatar answered Sep 28 '22 06:09

alecxe