Django i18n setlang view gives Error 404

孤人 提交于 2019-12-12 16:05:58

问题


I am trying to translate a django app using the built in i18n. I have already marked the text to be translated and created and compiled the language files (.po/.mo) according to the tutorial and with out errors. I've also changed the USE_I18N to true in the settings file and added the following line into the urls.py as the tutorial instructed:

(r'^i18n/', include('django.conf.urls.i18n')),

I also defined a list of allowed languages in the settings.py, as instructed by the tutorial.

then i created a new page html template and copied in the code the tutorial gave for a language select page:

<form action="/i18n/setlang/" method="post">
 {% csrf_token %}
 <input name="next" type="hidden" value="{{ redirect_to }}" />
 <select name="language">
  {% get_language_info_list for LANGUAGES as languages %}
  {% for language in languages %}
   <option value="{{ language.code }}">{{ language.name_local }} ({{ language.code }})</option>
  {% endfor %}
 </select>
 <input type="submit" value="Go" />
</form>

that page works perfectly too, but when i click on "Go", it tells me there was an error loading the page:

Failed to load resource:http://localhost:8000/i18n/setlang/ 
the server responded with a status of 404 (NOT FOUND)

I tried changing the redirect by replacing the variable with the link, but that gave me the same result. I tried changing the form action path and the urls.py in case there was some double naming, which gave me the same error.

I have been reading through the tutorial and the readmes, as well as some of the i18n files and can't seem to find a reason for it not to work, and i would really appreciate an answer. thankyou


回答1:


Are you sure you used the correct urls.py, i.e. the one in your project root, and not in a subdirectory?

What happens if you change it to:

from django.http import HttpResponse
...
(r'^i18n/', lambda x: HttpResponse("Test")),

Can you go to http://localhost:8000/i18n/ after that?



来源:https://stackoverflow.com/questions/10898614/django-i18n-setlang-view-gives-error-404

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!