If a page that already uses {% csrf_token%} does not have a form, a cookie will not be sent. Therefore, as you noted, you will receive an error message when you try to use Ajax on such a page. This will lead to erratic behavior if you have a website with a combination of pages with various combinations of forms and ajax posts.
This has been reported and fixed: https://code.djangoproject.com/ticket/15354
The solution in the patch, to be deployed with 1.3.1, is the decor_cookie_csrf decorator. This decorator does not exist in 1.3 or 1.2.5
No need to wait, however. Just add this line to any view containing AJAX, a CSRF form message:
request.META["CSRF_COOKIE_USED"] = True
Example:
def calculator(request): request.META["CSRF_COOKIE_USED"] = True return render_to_response('calc.html', {'form': CalcForm()})
FYI is exactly what this decorator does.
Teilo
source share