40๐
Pass the query string in the template:
<a href="{% url 'my_videos' %}?filter=others&sort=newest">My Videos</a>
16๐
Iโm not sure whether this was possible at the time of this question being asked, but if you have the data available to build the query string in your view and pass it as context, you can make use of the QueryDict class. After constructing it, you can just call its urlencode function to make the string.
Example:
from django.http import QueryDict
def my_view(request, ...):
...
q = QueryDict(mutable=True)
q['filter'] = 'other'
q['sort'] = 'newest'
q.setlist('values', [1, 2, 'c'])
query_string = q.urlencode()
...
query_string
will now have the value u'filter=other&sort=newest&values=1&values=2&values=c'
- [Django]-Visual Studio Code: Intellisense not working
- [Django]-Custom form validation
- [Django]-Django apps aren't loaded yet when using asgi
12๐
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
if my_videos:
return HttpResponseRedirect( reverse('my_videos') + "?filter=others&sort=newest")
- [Django]-How to reverse a name to a absolute url in django template?
- [Django]-Distributed task queues (Ex. Celery) vs crontab scripts
- [Django]-Docker โ Can't access Django server
5๐
Here are two smart template tags for modifying querystring parameters within the template:
- [Django]-Django filter queryset __in for *every* item in list
- [Django]-Django Rest Framework POST Update if existing or create
- [Django]-Django โ makemigrations โ No changes detected
2๐
In templates you could do something like the following:
{% url 'url_name' user.id %}
{% url 'url_name'%}?param=value
{% "/app/view/user_id" %}
In the first one, the querystring will be in the form of โhttp://localhost.loc/app/view/user_idโ
In the second one, the query string should be in the form of โhttp://localhost.loc/app/view?param=valueโ
In the third one, it is simple however I am recommending the first one which depends on the name of the url mappings applied in urls.py
Applying this in views, should be done using HttpResponseRedirect
#using url_names
# with standard querystring
return HttpResponseRedirect( reverse('my_url') + "?param=value")
#getting nicer querystrings
return HttpResponseRedirect(reverse('my_url', args=[value]))
# or using relative path
return HttpResponseRedirect( '/relative_path' + "?param=value")
Hint: to get query string value from any other view
s = request.GET.get('param','')
- [Django]-Django ModelChoiceField optgroup tag
- [Django]-How do I refresh the values on an object in Django?
- [Django]-Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
1๐
Redirect to the URL you want using HttpResponseRedirect
from django.http import HttpResponseRedirect
[...]
if my_videos:
return HttpResponseRedirect(request.path + "?filter=others&sort=newest")
- [Django]-Fields.E304 Reverse accessor clashes in Django
- [Django]-Converting timezone-aware datetime to local time in Python
- [Django]-Django admin page Removing 'Group'