I have just started on Django and am quite new to the whole thing.
I went through the whole tutorial on https://docs.djangoproject.com/en/1.7/intro/tutorial03/ , which involves settings up the database and writing a simple form.
To start off my journey in Django, I plan to write a simple app that runs on localhost. And I have faced a issue in passing inputs form a form.
I have created a Name class in the models.py with 1 attribute
#name of the person
value = models.CharField(max_length=50)
In my index link: http://localhost:8000/helloworld/, it contains a simple 1-input-field form as follows:
<form method="post" action="{% url 'helloworld:hello' %}">
{% csrf_token %}
Enter Name: <input size="80" name="link" type="text">
<button type="submit">Submit</button>
</form>
The aim of the form is to return the input data to the same link (http://localhost:8000/helloworld/) with a input message of:
"Welcome [NAME], Hello World"
In my views.py, the following method is written:
def hello(request,name):
p = get_object_or_404(Link, pk=name)
try:
input_link = p.choice_set.get(pk=request.POST['link'])
except (KeyError, Link.DoesNotExist):
return render(request, 'helloworld/index.html',{
'error_message': "You did not enter a name",
})
else:
return HttpResponseRedirect(reverse('helloworld:index', args=(p.value)))
How ever if I access the page http://localhost:8000/helloworld/, and entered a data in the field and click submit, it brings me to the page
Page not found (404)
Request Method: POST
Request URL: http://localhost:8000/helloworld/url%20'helloworld:hello'
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^helloworld/ ^$ [name='index']
^helloworld/ ^(?P<pk>\d+)/$ [name='detail']
^helloworld/ ^(?P<pk>\d+)/results/$ [name='results']
^helloworld/ ^(?P<question_id>\d+)/vote/$ [name='vote']
^admin/
The current URL, helloworld/url 'helloworld:hello', didn't match any of these.
The content in the urls.py was from https://docs.djangoproject.com/en/1.7/intro/tutorial04/#amend-urlconf
As requested, the content of urls.py:
from django.conf.urls import patterns, url
from domparser import views
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
)
May I know how do I go about solving this issue?
Thanks!
action=""?