syv
syv

Reputation: 3608

URL patterns for GET

I want to define url pattern for the below url and read these parameters in views

http://example.com/user/account?id=USR1045&status=1

I tried

url(r'^user/account/(?P<id>\w+)/(?P<status>\d+)/$', useraccount),

In Views

request.GET ['id']
request.GET ['status']

but it is not working, please correct me.

Upvotes: 1

Views: 496

Answers (2)

grigno
grigno

Reputation: 3198

Querystring params and django urls pattern are not the same thing.

so, using django urls pattern:

your url:

http://example.com/user/account/USR1045/1

urls.py

url(r'^user/account/(?P<id>\w+)/(?P<status>\d+)/$', views.useraccount)

views.py

def useraccount(request, id, status):

Upvotes: 0

Burhan Khalid
Burhan Khalid

Reputation: 174708

django url patterns do not capture the query string:

The URLconf searches against the requested URL, as a normal Python string. This does not include GET or POST parameters, or the domain name.

For example, in a request to http://www.example.com/myapp/, the URLconf will look for myapp/.

In a request to http://www.example.com/myapp/?page=3, the URLconf will look for myapp/.

The URLconf doesn’t look at the request method. In other words, all request methods – POST, GET, HEAD, etc. – will be routed to the same function for the same URL.

So, with that in mind, your url pattern should be:

url(r'^user/account/$', useraccount),

In your useraccount method:

def useraccount(request):
    user_id = request.GET.get('id')
    status = request.GET.get('status')

    if user_id and status:
        # do stuff
    else:
        # user id or status were not in the querystring
        # do other stuff

Upvotes: 2

Related Questions