Paul Tarjan
Paul Tarjan

Reputation: 50642

Force query parameters to be matched in urls.py

I want the WHOLE url after the slash to be passed to a script. If I do :

url(r'^(?P<id>.*)$', alias.get, name="alias"),

Then I only get the path component and not the query parameters passed to my function. I then have to :

def urlencode(dict) :
    if len(dict) == 0 : return ""
    params = {}
    for k, v in dict.items() :
        params[k] = v.encode('utf-8')

    return "?" + urllib.urlencode(params)

def get(id) :
    id += urlencode(request.GET)

I am doing this for a lot of my views and I keep forgetting it and creating bugs. Is there any way to tell my urls.py to match everything including the query string?

Upvotes: 1

Views: 1015

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599778

No, there's no way of doing that. The GET parameters aren't passed to urls.py.

I wonder why you need to do this though. Why are so many of your views dependent on GET querystrings? Why don't you use the Django way of doing things, which is to make the parameters part of the URL itself, rather than as querystrings.

Upvotes: 1

krubo
krubo

Reputation: 6416

Inside your view, you can get the URL including the query string with request.get_full_path().

There's also request.build_absolute_uri() if you want to include the http://servername.com part.

Upvotes: 3

Related Questions