Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top