cant pass the "CSRF verification failed. Request aborted." error

160 views
Skip to first unread message

dk

unread,
Oct 8, 2014, 1:22:24 AM10/8/14
to django...@googlegroups.com
I try
render to context with the extra argument
importing the class csrf_protect and used as decorator.

and looks like if you use the "render" function should be the easiest way since will take care of everything.
I also tried removing the tags from the html and I still get the error.

I am missing to put something?


2 extra questions. 
can I use my own forms with django done in a html file? or they have to be django forms?  (as I understand the django forms are premade so its easy to use them and render the windget? like a calendar widget)

now that I have the form and the submit how can I pass the info of the form to the next webpage,  example
so it say  thank you for voting + the email they typed in the form.


thanks guys =p


my views
#import django stuff
import django
from django.shortcuts import render, render_to_response
from django.http import HttpResponse
from django.template import Template, Context

from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from django.views.decorators.csrf import csrf_protect
from django.template import RequestContext
#importing tables---
from polls.models import Restaurant
from polls.models import Dish
#importing forms---
import polls.forms_DK

def main_site(request):
   
return render_to_response('initial_page.html')


def vote(request):
   
if request.method == "POST":
        form
= polls.forms_DK.NameForm(request.POST)
       
if form.is_valid():
           
return HttpResponseRedirect('/thanks')
   
else:
        form
= polls.forms_DK.NameForm()
    django
.setup()
    all_restaurants
= Restaurant.objects.all()
   
return render_to_response("vote_form.html", {"all_restaurants": all_restaurants, "form": form})

def thanks(request):
    c
= {"thanks_body": "thanks_body"}
   
return render(request, "thanks.html", c)


my forms.py
from django import forms
from polls.models import Restaurant
import django

class NameForm(forms.Form):
 your_email
= forms.EmailField()
 django
.setup()
 all
= Restaurant.objects.all()
 options_ratio
= []
 
for i in all:
 mini_list
= []
 mini_list
.append(i.id)
 mini_list
.append(i.name)
 options_ratio
.append(mini_list)
 ratio
= forms.ChoiceField(choices=options_ratio, widget=forms.RadioSelect())


the html
{% extends "initial_page.html" %}

{% block body %}
   
<p>Vote for the restaurant of your choise</p>

    <form action="/
polls/vote/thanks/" method="post">{% csrf_token %}
        {{ form }}
        <input type="
submit" value="Submit" />
    </form>
{% endblock %}









restaurants.zip

dk

unread,
Oct 8, 2014, 1:23:23 AM10/8/14
to django...@googlegroups.com
I am using 1.7

Collin Anderson

unread,
Oct 8, 2014, 8:16:33 AM10/8/14
to django...@googlegroups.com
cant pass the "CSRF verification failed. Request aborted." error
Does it say why?
 
and looks like if you use the "render" function should be the easiest way since will take care of everything.
I also tried removing the tags from the html and I still get the error.
Exactly. Does this work?
return render(request, "vote_form.html", {"all_restaurants": all_restaurants, "form": form})

 2 extra questions. 
can I use my own forms with django done in a html file? or they have to be django forms?  (as I understand the django forms are premade so its easy to use them and render the widget? like a calendar widget)
Yes, you can do whatever you want :) The built in django forms are especially helpful for validating the data in the back end, but even if you do that, you can still hand-code the html.
  
now that I have the form and the submit how can I pass the info of the form to the next webpage,  example
so it say  thank you for voting + the email they typed in the form.
There are a few ways to do it that I can think of:
For simplicity, I usually do something like redirect them to: /thanks/?email={{ their email }}. Then I can use that info in the template.
You could also store their email address in request.SESSION.

dk

unread,
Oct 9, 2014, 5:51:49 PM10/9/14
to django...@googlegroups.com
Ah....   nice trick, store the variables into the url and then pass the url with some regular expression.
=)



def vote(request):
   
if request.method == "POST":
        form
= polls.forms_DK.NameForm(request.POST)
       
if form.is_valid():
            your_email = form.cleaned_data["your_email"]
            ratio = str(form.cleaned_data["ratio"])
            adress = "thanks/"+your_email+"/"
           
return HttpResponseRedirect(adress)

   
else:
        form
= polls.forms_DK.NameForm()
    django
.setup()
    all_restaurants
= Restaurant.objects.all()

   
#return render_to_response("vote_form.html", {"all_restaurants": all_restaurants, "form": form})

   
return render(request, "vote_form.html", {"all_restaurants": all_restaurants, "form": form})


ok now to learn how to catch that url
will something like this would work?
url(r'^thanks/(?P<your_email>w+)/$', views.thanks),


Collin Anderson

unread,
Oct 9, 2014, 7:45:09 PM10/9/14
to django...@googlegroups.com
ok now to learn how to catch that url
will something like this would work?
url(r'^thanks/(?P<your_email>w+)/$', views.thanks),

That should work in theory, although the "w" in the regular expression won't match the @ sign or the periods.

If you use the /thanks/?email=yo...@email.etc you can use this syntax in the urls:
    url(r'^thanks/$', views.thanks),
And this in the view:
def thanks(request):
    render
(request, 'thanks.html', {'email': request.GET.get('email')})

Reply all
Reply to author
Forward
0 new messages