Include request context:

from django.template import RequestContext

...and then pass it in with the render. That should include the token
in the template.

return render_to_response('add_pair.html', {},
context_instance=RequestContext(request))

On Apr 26, 12:45 pm, jeff <jeffre...@gmail.com> wrote:
> so i have some views and forms done without using the
> CsrfViewMiddleware. now i am converting to a 1.2 project using this
> middleware. read the doc, looked for examples, still can't process the
> POST function from my form.
>
> the example i have is very simple. hoping for someone who knows this
> to take a quick look and tell me where i went wrong:
>
> i get this message:
> ==================
> 403 Forbidden
> CSRF verification failed. Request aborted.
> Help
> Reason given for failure:
>     No CSRF or session cookie.
>
> followed the links and instructions provided.
>
> my template now has:
> ==================
> {% block other_cont %}
>         <div id="oth_cont">
>         <p>page to add a new pair.</p></br></br>
>         <form action ="." method="POST">{% csrf_token %}
>                 <table border="1">
>                         {{ form.as_table }}
>                 </table>
>                 <p><input type="submit" value="Submit"></p>
>         </form>
>         </div>
> {% endblock %}
>
> and my view has this:
> ===================
> def add_pair(request):
>         # these are the basic locals we need to set for the template to
> populate
>         page_title='Add a Pair'
>         if request.method == 'POST':
>                 form=SymForm(request.POST)
>                 if form.is_valid():
>                         s1=form.cleaned_data['sym1']
>                         s2=form.cleaned_data['sym2']
>                         s=Pair(sym1=s1,sym2=s2)
>                         s.save()
>                         url='/sym/added/'+s1+"/"+s2+"/"
>                         return HttpResponseRedirect(url)
>         else:
>                 form = SymForm()
>         c = {}
>         c.update(csrf(request))
>         return render_to_response('add_pair.html', locals())
>
> and form was not changed:
> ========================
> class SymForm(forms.Form):
>         sym1=forms.CharField()
>         sym2=forms.CharField()
>
>         def clean_sym1(self):
>                 cleaned_data = self.cleaned_data
>                 sym1 = cleaned_data.get("sym1")
>                 ok=False
>                 if re.match("^[A-Z.]{1,6}$",sym1):
>                         ok=True
>                 if not ok:
>                         raise forms.ValidationError("A symbol must be 1-6 
> characters in
> length, caps only")
>                 return sym1
>
>         def clean_sym2(self):
>                 cleaned_data = self.cleaned_data
>                 sym2 = cleaned_data.get("sym2")
>                 ok=False
>                 if re.match("^[A-Z.]{1,6}$",sym2):
>                         ok=True
>                 if not ok:
>                         raise forms.ValidationError("A symbol must be 1-6 
> characters in
> length, caps only")
>                 return sym2
>
> my settings.py has the middleware:
> ==================
> MIDDLEWARE_CLASSES = (
>     'django.middleware.common.CommonMiddleware',
>     'django.contrib.sessions.middleware.SessionMiddleware',
>     'django.middleware.csrf.CsrfViewMiddleware',
>     'django.contrib.auth.middleware.AuthenticationMiddleware',
>     'django.contrib.messages.middleware.MessageMiddleware',
> )
>
> please could you help???
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group 
> athttp://groups.google.com/group/django-users?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to