20๐
So, write a template tag around this:
from urlparse import urlparse, urlunparse
from django.http import QueryDict
def replace_query_param(url, attr, val):
(scheme, netloc, path, params, query, fragment) = urlparse(url)
query_dict = QueryDict(query).copy()
query_dict[attr] = val
query = query_dict.urlencode()
return urlunparse((scheme, netloc, path, params, query, fragment))
For a more comprehensive solution, use Zachary Voaseโs URLObject 2, which is very nicely done.
Note:
The urlparse
module is renamed to urllib.parse
in Python 3.
26๐
I did this simple tag which doesnโt require any extra libraries:
@register.simple_tag
def url_replace(request, field, value):
dict_ = request.GET.copy()
dict_[field] = value
return dict_.urlencode()
Use as:
<a href="?{% url_replace request 'param' value %}">
It wil add โparamโ to your url GET string if itโs not there, or replace it with the new value if itโs already there.
You also need the RequestContext request instance to be provided to your template from your view. More info here:
http://lincolnloop.com/blog/2008/may/10/getting-requestcontext-your-templates/
- [Django]-Django Multiple Authentication Backend for one project
- [Django]-Name '_' is not defined
- [Django]-How do I install psycopg2 for Python 3.x?
10๐
I improved mpafโs solution, to get request directly from tag.
@register.simple_tag(takes_context = True)
def url_replace(context, field, value):
dict_ = context['request'].GET.copy()
dict_[field] = value
return dict_.urlencode()
- [Django]-Python 3 list(dictionary.keys()) raises error. What am I doing wrong?
- [Django]-How do I install psycopg2 for Python 3.x?
- [Django]-Passing STATIC_URL to file javascript with django
4๐
This worked pretty well for me. Allows you to set any number of parameters in the URL. Works nice for a pager, while keeping the rest of the query string.
from django import template
from urlobject import URLObject
register = template.Library()
@register.simple_tag(takes_context=True)
def url_set_param(context, **kwargs):
url = URLObject(context.request.get_full_path())
path = url.path
query = url.query
for k, v in kwargs.items():
query = query.set_param(k, v)
return '{}?{}'.format(path, query)
Then in the template:
<a href="{% url_set_param page=last %}">
- [Django]-Name '_' is not defined
- [Django]-Django Multiple Authentication Backend for one project
- [Django]-Python 3 list(dictionary.keys()) raises error. What am I doing wrong?
1๐
There are a number of template tags for modifying the query string djangosnippets.org:
http://djangosnippets.org/snippets/553/
http://djangosnippets.org/snippets/826/
http://djangosnippets.org/snippets/1243/
I would say those are the most promising looking. One point in all of them is that you must be using django.core.context_processors.request
in your TEMPLATE_CONTEXT_PROCESSORS
.
- [Django]-Django REST Framework (DRF): Set current user id as field value
- [Django]-Django: For Loop to Iterate Form Fields
- [Django]-Django apps aren't loaded yet when using asgi
- [Django]-Cannot access django app through ip address while accessing it through localhost
- [Django]-AccessDenied when calling the CreateMultipartUpload operation in Django using django-storages and boto3
- [Django]-Django Multiple Authentication Backend for one project
0๐
In addition to the snippets mentioned by Mark Lavin, Hereโs a list of other implementations I could find for a Django template tag which modifies the current HTTP GET query string.
On djangosnippets.org:
- #2237 Manipulate URL query strings using context variables using a template tag by JHsaunders
- #2332 Querystring Builder โ create urls with GET params by jibberia
- my favorite: #2413 Yet another query string template tag by atms
- #2428 Add GET parameters from current request by naktinis
On PyPI:
- django-spurl by Jamie Matthews
- django-urltags by Calloway Project/Corey Oordt
- the add_query_param filter in django-rest-framework by Tom Christie
On GitHub:
- [Django]-In django, how do I sort a model on a field and then get the last item?
- [Django]-Django REST Framework (DRF): Set current user id as field value
- [Django]-Why won't Django use IPython?