Arpan Kc
Arpan Kc

Reputation: 938

Sending multiple parameters in url as GET request in django?

I am sending a GET request from jquery as:

http://127.0.0.1:8000/viewspecs/itemdetails?param1="+variable1+"&param2="+ variable2

The urls.py file in django for this section looks something like:

url(r'^viewspecs/itemdetails?param1=(?P<specs_search_item>[\w\+%_ ./-]+)&param2=(?P<item_price>[0-9])$', views.specsView),

When I access the address ,I get a page not(404) error. why ?

Upvotes: 5

Views: 11627

Answers (2)

Satyam Shree
Satyam Shree

Reputation: 51

If You are going to use this method

def specsView(request):
    param1 = request.GET['param1']
    param2 = request.GET['param2']

use .get() function

def specsView(request):
    param1 = request.GET.get('param1')
    param2 = request.GET.get('param2')

as this will not throw any errors even if the parameters are missing (or optional)

Upvotes: 0

Nishant Nawarkhede
Nishant Nawarkhede

Reputation: 8390

Your url should be,

url(r'^viewspecs/itemdetails/$', views.specsView),

And view will be like,

def specsView(request):
    param1 = request.GET['param1']
    param2 = request.GET['param2']

And if you want to pass parameters as,

http://127.0.0.1:8000/viewspecs/itemdetails/param1/param2

then urls will be,

url(r'^viewspecs/itemdetails/(?P<param1>[\w-]+)/(?P<param2>[\w-]+)/$', views.specsView),

view will be like this,

def specsView(request, param1, param2):
    pass 

Upvotes: 13

Related Questions