5

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 ?

3
  • Possible duplicate of Django accepting GET parameters Commented Jul 25, 2016 at 7:36
  • why not define url like this viewspecs/itemdetails and access using viewspecs/itemdetails?param=search_item Commented Jul 25, 2016 at 8:05
  • In the same way how would you accomplish this for two parameters? Commented Jul 25, 2016 at 8:10

2 Answers 2

13

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 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man! This solved my problem. Learned something new today. By the way I found the first method easier .
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.