views:

225

answers:

2

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?

+3  A: 

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.

+1  A: 

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.

Daniel Roseman
My urls look like : `http://example.com/alias/http://stackoverflow.com/users/90025/paul-tarjan`. That is, someone else posts in their url at the end of mine. Often times the posted in URL has query patterns which I want to capture as part of the "alias" as well.
Paul Tarjan