Django i18n setlang视图提供错误404 [英] Django i18n setlang view gives Error 404
问题描述
我正在使用内建的i18n来翻译一个django应用程序。我已经根据教程标记了要翻译的文本,并创建和编译了语言文件( .po / .mo
),并附带了错误。我还在设置文件中将 USE_I18N
更改为true,并按教程指示将以下行添加到urls.py中:
(r'^ i18n /',include('django.conf.urls.i18n')),
pre>
我还按照本教程的指示在settings.py中定义了允许的语言列表。
然后我创建了一个新的页面html模板,并复制了教程给出的语言选择页面的代码:
< form action =/ i18n / setlang /method =post>
{%csrf_token%}
< input name =nexttype =hiddenvalue ={{redirect_to}}/>
< select name =language>
{%get_language_info_list for LANGUAGES as language%}
{%for language in languages%}
< option value ={{language.code}}> {{language.name_local }}({{language.code}})< / option>
{%endfor%}
< / select>
< input type =submitvalue =Go/>
< / form>
该页面也很完美,但是当我点击Go ,它告诉我加载页面时出错:
无法加载资源:http:// localhost:8000 / i18n / setlang /
服务器响应状态为404(NOT FOUND)
I尝试通过用链接替换变量来改变重定向,但这给了我相同的结果。我尝试更改表单操作路径和urls.py,以防万一有一些双重命名,这给我同样的错误。
我一直在阅读本教程和readmes以及一些i18n文件,似乎找不到它不起作用的原因,我真的很感激一个答案。 thankyou
解决方案您确定使用正确的urls.py,即项目根目录中的那个,而不是子目录?
如果您将其更改为:
from django .http import HttpResponse
...
(r'^ i18n /',lambda x:HttpResponse(Test)),
之后可以去
http:// localhost:8000 / i18n /
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 theUSE_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
解决方案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?这篇关于Django i18n setlang视图提供错误404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!