Forums

Built-in view set_language doesn't work

Hello. I switch language in my app with built-in view set_language. It works on my local machine but it doesn't work here. Code from my template:

<form action="{% url 'set_language' %}" method="post">{% csrf_token %}
    <input name="next" type="hidden" value="{{ redirect_to }}" />
    <select name="language" id="language">
         {% get_current_language as LANGUAGE_CODE %}
         {% get_available_languages as LANGUAGES %}
         {% get_language_info_list for LANGUAGES as languages %}
         {% for language in languages %}
             <option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected="selected"{% endif %}>
             {{ language.name_local }} ({{ language.code }})
             </option>
         {% endfor %}
    </select>
</form>

And I have this in my settings, because I need only 2 languages:

LANGUAGES = [
    ('en', _('English')),
    ('ru', _('Russian')),
]

Also, I noticed that with this form I can switch language in Django Admin.

It's hard to tell what might be going wrong without a little more information. Do you have a URL that shows the problem?

What do you mean by "URL that shows the problem"? Just URL on my web app? http://evgenq.pythonanywhere.com/

I see, thanks.

I tried changing the language on your site, and it looks like the correct view is being called, and is returning with a 200 status code, so it thinks it has worked properly. I can also see that it's staying set when I refresh the page, so it's clearly setting the language correctly in the session.

Given that, the only possibility I can think of is that the code that gives the translations to Russian (the message files) isn't present on PythonAnywhere, or that they're not being found. Are you sure they're there? Have you compiled them on PythonAnywhere?

I forget to compile them. Now it works. Thank you.

Great! Glad we were able to work it out.