28

I have a function that calls another function and passes a query string to the end of the URL.

How would I accomplish the following --

if my_videos:
    return render(request, template, {kwargs}) + "?filter=others&sort=newest"

Note: I do not care if I use render or not, its use is merely to point out what I'm trying to accomplish. Thank you.

2

6 Answers 6

41

Pass the query string in the template:

<a href="{% url 'my_videos' %}?filter=others&sort=newest">My Videos</a>
Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't this end up with a trailing slash like this: example.com/videos/?filter=other&sort=newest?
This fails when {% url 'my_videos' %} already contains query parameters.
17

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'

Comments

12
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect


if my_videos:
    return HttpResponseRedirect( reverse('my_videos') + "?filter=others&sort=newest")

Comments

5

Here are two smart template tags for modifying querystring parameters within the template:

Comments

3

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','')

4 Comments

"query string" != "url param"
What's the third form supposed to mean? What's the template tag there?
The third form is mostly used in the views, GET itself is the dictionary (HashMap as in Java) object while get is the method that is getting the value based on the passed key 'param' from the dictionary, the second param is the default value in case the key is not found in the dictionary. you also have the option to call it like this request.GET["param"] without specifying any default values
@HarryLee yes you are right however I tried to give all possible ways in my answer to get values either by using query string or url param (you can say both have them have the same semantics however it it not the same syntax or definition) .. thanx for your comment
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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.