'CSRF Verification Failed' in Django Translation

69 views
Skip to first unread message

Veni Kunche

unread,
Nov 23, 2016, 7:42:09 AM11/23/16
to Django users
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
Reply all
Reply to author
Forward
0 new messages