krips89
krips89

Reputation: 1743

form action not working in django

I have django 1.4 and I am following a tutorial which uses an older version of django. Its a simple tutorial which creates a wiki app with Page as model.

The problem is that the view function corresponding to a POST method in a form is not getting invoked.

This is the content in the urls.py:

url(r'^wikicamp/(?P<page_name>[^/]+)/edit/$', 'wiki.views.edit_page'),
url(r'^wikicamp/(?P<page_name>[^/]+)/save/$', 'wiki.views.save_page'),
url(r'^wikicamp/(?P<page_name>[^/]+)/$', 'wiki.views.view_page'),

This is the content of the template edit.html:

<from method = "get" action="/wikicamp/{{page_name}}/save/">
{% csrf_token %}
    <textarea name = "content" rows="20" cols="60">
    {{content}}
    </textarea>
    <br/>
    <input type="submit" value="Save Page"/>
</form>
<a href="/wikicamp/{{page_name}}/save/">this is link to save</a>

And this is the content in views.py:

def edit_page(request, page_name):
try:
    page = Page.objects.get(pk=page_name)
    content = page.content
except Page.DoesNotExist:
    content = ""
return render_to_response("edit.html", {"page_name":page_name, "content":content}, context_instance=RequestContext(request))

def save_page(request, page_name):
    return HttpResponse("You're looking at the page %s." % page_name)

I initially I was getting csrf related error and I then tried all the fixes provided in https://docs.djangoproject.com/en/dev/ref/contrib/casrf/ and followed many many stackoverflow question related to POST and django. Now nothing happens when I click the 'Save Page' button, nothing! Not even any request being sent from the form (Using firebug to track the HTTP request and response)

Upvotes: 3

Views: 4678

Answers (3)

Henrik Andersson
Henrik Andersson

Reputation: 47222

There are some spelling mistakes, such as from instead of form. Also the form is malformed.

Change:

<a href="/wikicamp/{{page_name}}/save/">this is link to save</a>

to

<input type="submit" value="Save Page" />

And thirdly, change the method= "get"to method="POST".

The entire form should look like this

<form method = "POST" action="/wikicamp/{{page_name}}/save/">
{% csrf_token %}
    <textarea name = "content" rows="20" cols="60">
    {{content}}
    </textarea>
    <br/>
    <input type="submit" value="Save Page"/>
</form>

Also what @DanielRoseman said. But hey, it might come further down the road.

Upvotes: 0

Rohan
Rohan

Reputation: 53386

You may need to change method to "POST" in your form.

<from method = "get" action="/wikicamp/{{page_name}}/save/">

to

<form method = "post" action="/wikicamp/{{page_name}}/save/">

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 600026

You have a typo in your HTML: from instead of form.

You may realize this, but that code won't really save anything. I'm not sure what blog you are following, but you would be better-off following the official Django tutorial in the documentation, then reading the forms docs.

Upvotes: 4

Related Questions