I'm trying to write a webpage that allows user to input an "Idea" and save it to the database. I want to use the Post method of the form. I tried following the instructions on the django page, but the authentication still fails.
This is my form code:
<form action="/" method="post">
{% csrf_token %}
<p> Title: </p> <input type="text" name="title"> </br>
<p> Details: </p> <input type="text" name="details">
<input type="submit" value="Post">
</form>
My URLconf redirects /
to Ideas.views.home
with the line
url(r'^$', 'Ideas.views.home' , name='home')
The view itself is
from django.shortcuts import render, render_to_response
from django.http import HttpResponse
from django.template import loader, Template, Context, RequestContext
from django.core.context_processors import csrf
from Ideas.models import Idea
# Create your views here.
def home(request):
if request.method == 'POST':
submitted = Idea.objects.create(title=request.POST['title'], detail=request.POST['details'], votes=0)
submitted.save()
m = RequestContext(request, {"passed_ideas": Idea.objects.all()})
return render_to_response('basic.html', m)
I have also tried doing this:
from django.shortcuts import render, render_to_response
from django.http import HttpResponse
from django.template import loader, Template, Context, RequestContext
from django.core.context_processors import csrf
from Ideas.models import Idea
# Create your views here.
def home(request):
c = {}
c.update(csrf(request))
if request.method == 'POST':
submitted = Idea.objects.create(title=request.POST['title'], detail=request.POST['details'], votes=0)
submitted.save()
c['passed_ideas'] = Idea.objects.all()
return render_to_response('basic.html', c)
How do I get the CSRF authentication to work?