1π
The error comes because when you call the URL /NECapp/offers/?zip=55104,since you have passed zip as query string and not as parameter, the second offers URL pattern gets called and since you haveβnt given any default arguments it shows the error.
Exception Value: offers() takes exactly 2 arguments (1 given)
def offers(request, zip):
return HttpResponse('you entered %zip' %(zip))
You need to fix two things:
1)As per your URL pattern,your request URL should be /NECapp/offers/55104/
2)Whenever second URL pattern gets called you will get error.Fix it by default args.
def offers(request, zip=""):
return HttpResponse('you entered %zip' %(zip))
if you are trying to post the data via form,put method=βPOSTβ in your form tag and you dont need two URI patterns as all form data will be available in your request.POST dictionary.
HTML:
<form action="/NECapp/offers/" method="post">
URLS.py:
url(r'^offers/$', 'views.offers'),
Views.py:\
def offers(request):
zip=request.POST.get("zip")
return HttpResponse('you entered %zip' %(zip))
0π
Look closely at your urls.py
url(r'^offers/(?P<zip>\d{5})/$', 'views.offers', name='offers'),
conflicts with
url(r'^offers/$', 'views.offers'),
HTH!
- [Answer]-Template changes are not appearing in Django admin site
- [Answer]-Subclassing django model doesn't carry the Manager
- [Answer]-How to apply/make available my modified Django class-based generic view mixin site-wide?
- [Answer]-How to make testing urls?
0π
To make it work (NECapp/offers/
URL), you will need to just add default value to zip
.
This is because you execute views.offers
view in two cases, depending on the requested URI:
- When there is something after β
/offers/
β (you have zip code passed) β for this you have your url pattern second from the end (the one withname='offers'
), - When there is nothing after β
/offers/
β (for this you have your last URL pattern).
Your view would look similarly to this:
def offers(request, zip=None):
if zip is None:
# No zip has been set (/offers/)
return HttpResponse('you did not enter anything')
else:
# Some zip has been set (/offers/some_zip)
return HttpResponse('you entered %zip' %(zip))
EDIT:
Vivek correctly noted, that you are invoking http://localhost:8000/NECapp/offers/?zip=55104
instead of http://localhost:8000/NECapp/offers/55104
. This is why you get your last URI pattern matched (the one with r'^offers/$'
rule) and the view is invoked without parameters. If you will use my solution, you will get βyou did not enter anything
β in response, because you are not passing zip
within URL (actually you are, but in query string, and you should pass it in the main part of the URL instead, if you want to use your URLconf).
So, in short, use my solution and pass zip
in URL like that: http://localhost:8000/NECapp/offers/55104
. Alternatively you can just get rid of this pattern with zip
and just use request.GET.get(zip)
to get zip
from query string.
- [Answer]-Django: is it possible to exclude ForeignKey in derived model
- [Answer]-Moving from Sqlite3 to mysql β how to copy all data
- [Answer]-Accessing a Form's cleaned data from a signals callback?
- [Answer]-Passing variables from template to view