Good day. The Django documentation has proposed code for switching languages. Actually, here it is:

<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 %} {% 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> <input type="submit" value="Go"> </form> 

No, I did not forget {% load i18n%}. Yes, I connected LocaleMiddleware in settings.py.

To complete the picture, I merge urls.py:

 from django.conf.urls import url, include from django.conf.urls.i18n import i18n_patterns from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^i18n/', include('django.conf.urls.i18n')), ] urlpatterns += i18n_patterns( url(r'^', include('myapp.urls')), # остальные url'ки ) 

And what happens at startup? And nothing. Select the desired language, click on the button and in addition to reloading the page, no action and changes. The language is successfully changed only by manually editing the URL in the address bar. I am reviewing something and do not understand that I forgot something?

./manage.py show_urls

 /en/ app.views.home /en/<category_slug>/ app.views.categories categories /en/<category_slug>/<article_slug>/ app.views.artices articles /en/media\/<path> django.views.static.serve /i18n/setlang/ django.views.i18n.set_language set_language ... # все ссылки, связанные с админкой /ru/ app.views.home /ru/<category_slug>/ app.views.categories categories /ru/<category_slug>/<article_slug>/ app.views.artices articles /ru/media\/<path> django.views.static.serve 

When changing the language in the network, setlang / first appears with the following status:

setlang /

Then the language that was selected before switching over appears. In this case, the switch was to English, but still Russian remained with the following status:

enter image description here

I want to add that without url_patterns the switch works with a bang. Also, if it is turned on and the language is deleted in the address bar, then after a reboot, it is the one that was sent in the switch (that is, it is pulled out of the cache or session). Somewhere there is a problem with url_patterns or something not connected.

  • It would be nice to look at the source of the page, which is generated by your code. It is important. You can see in the developer’s tools in any browser. Particular attention to the form of switching. - Mikhail Alekseevich
  • I hope you about this (added a photo in the description of the question). - Jimmy
  • stackoverflow.com/questions/1275486/… ./manage.py showurls show :) how to post @Seti Volkylany - Mikhail Alekseevich
  • Added to the question description - Jimmy
  • In urls there is no i18n ... But in theory, middleware can do without urlpatterns, so there are no redirection codes in the browser developer console when changing the language? Should work as HttpRedirectResponse, respectively, and the server response code for 200 - Mikhail Alekseevich

0