jorgebastida / django-dajax

Easy to use library to create asynchronous presentation logic with django and dajaxice
http://dajaxproject.com/
BSD 3-Clause "New" or "Revised" License
346 stars 99 forks source link

How to set csrf for ajax form? #50

Closed mzu closed 11 years ago

mzu commented 11 years ago

Hi there.

When I request a form via django-dajax I get

warnings.warn("A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not using RequestContext.")

I there a way to set the csrf token in django-dajax using the request header (see https://docs.djangoproject.com/en/1.2/ref/contrib/csrf/#csrf-ajax).

What would be the best way to deal with this issue?

Thanks + great work!

Mischa

ghost commented 11 years ago

Hi mzu, I got the same warnings when I started to use Dajax. I've solved the issue using TemplateResponse in place of render_to_string, because you need to render the template with the request:

from django.template.response import TemplateResponse ... render = TemplateResponse(request, template, context).render().content

Bye.

mzu commented 11 years ago

Thanks esevece, I will try your approach!