Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending multiple parameters in url as GET request in django?

Tags:

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 ?

like image 760
Arpan Kc Avatar asked Jul 25 '16 07:07

Arpan Kc


People also ask

How do I pass multiple parameters in URL?

To add a parameter to the URL, add a /#/? to the end, followed by the parameter name, an equal sign (=), and the value of the parameter. You can add multiple parameters by including an ampersand (&) between each one.

How do I get parameters in GET request Django?

We can access the query params from the request in Django from the GET attribute of the request. To get the first or only value in a parameter simply use the get() method. To get the list of all values in a parameter use getlist() method.

What does form {% URL %} do?

{% url 'contact-form' %} is a way to add a link to another one of your pages in the template. url tells the template to look in the URLs.py file. The thing in the quotes to the right, in this case contact-form , tells the template to look for something with name=contact-form .


1 Answers

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 
like image 194
Nishant Nawarkhede Avatar answered Sep 29 '22 18:09

Nishant Nawarkhede