0

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!

2
  • can you show urls.py ? Commented Mar 26, 2015 at 6:15
  • Did you try action="" ? Commented Mar 26, 2015 at 6:25

2 Answers 2

1

Simply an answer for this type. If you want to post to same url you are currently now.Try this and chenge def hello(request, name) to def hello(request).

action=""

Otherwise if urls.py

urlpatterns = patterns('app.views',
url(r'^$', view="index", name="app_index"),

)

Try this

action="{% url app_index %}"

As i found, in yours you can apply

action="{% url helloworld:index %}"

Hope this helps

For your answer updated. Try this

<form method="post" action="{% url "index" %}"> 
Sign up to request clarification or add additional context in comments.

5 Comments

hi, i did that and it posted me to the same link with a empty page instead of displaying the field again. may i know how do you display the expected input in the new page?
That posted the data and reload the html. Then what you want after post?
in the input field i would enter a name and after clicking submit i wish to display the message, Welcome [NAME], Hello World!
k, thet put a condition in view, if request.method=='POST: do your operations request.POST[link], Then return HttpResponse("you message") or render new html with your data
actually i have already created a function in view as stated in my post above. how do I actually call it?
0

You need change this line

<form method="post" action="{% url 'helloworld:hello' %}">

to

<form method="post" action="{% 'helloworld:hello' %}">

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.