I am sending a GET request from jquery as:
http://127.0.0.1:8000/viewspecs/itemdetails?param1="+variable1+"¶m2="+ variable2
The urls.py file in django for this section looks something like:
url(r'^viewspecs/itemdetails?param1=(?P<specs_search_item>[\w\+%_ ./-]+)¶m2=(?P<item_price>[0-9])$', views.specsView),
When I access the address ,I get a page not(404) error. why ?
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.
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.
{% 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 .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With