Search code examples
djangodajaxice

Why do I get a '403 (Forbidden)' error with Dajaxice?


I just installed Dajaxice, but I keep having this error:

POST https://.../communication.isUserLogged/ 403 (Forbidden) dajaxice.core.js:163
Dajaxice: Something went wrong. 

on the server side:

"POST /dajaxice/communication.isUserLogged/ HTTP/1.1" 403 2282

I guess the server does not get the correct CSRF token, but I don't know how to fix the problem.

Dajaxice documentation says

Ensure that TEMPLATE_CONTEXT_PROCESSORS has django.core.context_processors.request.

but it is set by default in Django 1.5. I still get this warning:

The 'request' object must be accesible within the context. You must add 'django.contrib.messages.context_processors.request' to your TEMPLATE_CONTEXT_PROCESSORS and render your views using a RequestContext.

Here is my code: https://c9.io/arthursw/brain


Solution

  • according to Django's API (see also here) django.core.context_processors.request is not set by default:

    By default, TEMPLATE_CONTEXT_PROCESSORS is set to:

    ("django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.tz",
    "django.contrib.messages.context_processors.messages")
    

    In addition to these, RequestContext always uses django.core.context_processors.csrf.

    AFTER DISCUSSION...

    then, at the very end, you have just to add the following line at the end of your settings file:

    TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + ( 'django.core.context_processors.request', )