David542
David542

Reputation: 110382

Append a querystring to URL in django

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.

Upvotes: 28

Views: 35052

Answers (6)

beruic
beruic

Reputation: 5876

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'

Upvotes: 17

David542
David542

Reputation: 110382

Pass the query string in the template:

<a href="{% url 'my_videos' %}?filter=others&sort=newest">My Videos</a>

Upvotes: 41

Udi
Udi

Reputation: 30522

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

Upvotes: 5

Muhammad Soliman
Muhammad Soliman

Reputation: 23816

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

Upvotes: 3

Ahsan
Ahsan

Reputation: 11832

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


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

Upvotes: 12

Roshan Mathews
Roshan Mathews

Reputation: 5898

Redirect to the URL you want using HttpResponseRedirect

from django.http import HttpResponseRedirect

[...]

if my_videos:
    return HttpResponseRedirect(request.path + "?filter=others&sort=newest")

Upvotes: 1

Related Questions