Reputation: 7060
I am new to Django
I am trying to call a python function/view on the click of a submit button.
I have made the necessary changes to tell the url file where to look at.
This is the urls.py of my site and I am concerned with polls app.
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
]
This is the urls.py of polls app.
urlpatterns = [
# ex: /polls/
url(r'^$', views.index, name='index'),
# ex: /polls/5/
url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
# ex: /polls/5/results
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
# ex: /polls/5/vote
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
# ex: /test/
url('/test/$', views.test, name='test'),
]
I want to call a function called test when submit button is clicked.
Part of index.html
<form action="/polls/test/" method="get">
Find: <input type="text" name="" size="35">
<input type="submit" value="Add Stuff">
</form>
Now, when click of this button if get error stating Django tried these URL and cannot find it.
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/polls/test/
Using the URLconf defined in mysite.urls, in this order:
patterns, in this order:
^polls/ ^$ [name='index']
^polls/ ^(?P<question_id>[0-9]+)/$ [name='detail']
^polls/ ^(?P<question_id>[0-9]+)/results/$ [name='results']
^polls/ ^(?P<question_id>[0-9]+)/vote/$ [name='vote']
^polls/ /test/$ [name='test']
^admin/
The current URL, polls/test/, didn't match any of these.
Am I missing something very obvious here? I would really appreciate any kind of help.
Thanks.
Upvotes: 0
Views: 453
Reputation: 308939
Remove the leading /
from your test url. You should also add a ^
at the beginning of the regex, so that it matches test
but not xtest
.
url('^test/$', views.test, name='test'),
As an aside, you can use the url tag to prevent having to hardcode the url in your template.
<form action="{% url 'test' %}" method="get">
Upvotes: 3