2

In a urls.py file:

url(r'^api/user_info/(?P<username>[a-zA-Z\d]+)\&', 'corresponding.view')
url(r'^api/user_info/(?P<username>[a-zA-Z\d]+)', 'corresponding.view')

There will always be HTTP Get arguments to /api/user_info/username.

The problem is that in the corresponding.view function, username will evaluate to something like "myusername?clientversion=2.0", instead of it evaluating to "myusername" and request.GET['clientversion'] = "2.0".

The first url call is to try to catch the ampersand in there, but it doesn't help.

Thanks in advance.

Bill
  • 2,319
  • 9
  • 29
  • 36

1 Answers1

4

See this question; you don't access query parameters using Django's URLConf. Trying to process ? or & characters using the URL resolver will not lead to happiness.

Just use the following URL pattern:

url(r'^api/user_info/(?P<username>\w\+)/$', 'corresponding.view')

and access the clientversion query parameter in your corresponding.view() function:

def view(request):
    client_version = request.GET.get('clientversion')
    ...

Or, if you're saying it should be mandatory,

from django.http import Http404

def view(request):
    try:
        client_version = request.GET['clientversion']
    except KeyError:
        raise Http404
Community
  • 1
  • 1
supervacuo
  • 9,072
  • 2
  • 44
  • 61
  • Your response was very enlightening for me. Thank you. However I realized that my true problem was pure stupidity on my end: My client software was requesting the page with the HTTP GET parameters starting with '&' rather than '?'... Took me several hours before I noticed... just one of those days... – Bill Aug 22 '12 at 20:26