Wednesday, November 23, 2016

'CSRF Verification Failed' in Django Translation

Hi everyone,

I've set up translation for a Django app using this tutorial. I am using Python 3.4.5 and Django 1.9.10.  I used set_language to switch between the languages. Everything was working great locally. However, I get a "CSRF Verification Failed" error on our test server sometimes. I've tried the common suggestions that I've found online:
  • I verified that {% csrf_token %} is inside the form
  • I checked that 'django.middleware.locale.LocaleMiddleware' has been added
  • I do have url(r'^i18n/', include('django.conf.urls.i18n')), in urls.py
These are the settings that are different in the test environment:
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
SESSION_COOKIE_HTTPONLY = True
SECURE_CONTENT_TYPE_NOSNIFF = True
CSRF_COOKIE_HTTPONLY = True
SECURE_BROWSER_XSS_FILTER = True
X_FRAME_OPTIONS = 'DENY'
CORS_ORIGIN_ALLOW_ALL = True

The test environment is running on https. The application has other forms which do posts and those are working fine. It's only the set_language form that's causing the error.

My questions:
  1. Is there anything else that I should look into?
  2. Is there another way to switch the language besides set_language in a form like this?
    <form action="{% url 'set_language' %}" method="post">      {% csrf_token %}      <input name="next" type="hidden" value="{{ redirect_to }}" />      <select name="language">          {% get_current_language as LANGUAGE_CODE %}          {% get_available_languages as LANGUAGES %}          {% for lang in LANGUAGES %}              <option value="{{ lang.0 }}" {% if lang.0 == LANGUAGE_CODE %} selected="selected"{% endif %}>                  {{ lang.1 }} ({{ lang.0 }})              </option>          {% endfor %}      </select>      <input type="submit" value="Go" />  </form>
Thank you for your help! Let me know if I can clarify further.

-Veni

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1f077982-d7d4-4598-ab3c-cf0799eb7049%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment