Skip to main content
 首页 » 编程设计

python之静态网站的 Django 国际化

2025年05月04日33cloudgamer

我正在尝试使用 Django 和 Python 制作双语(意大利语和英语)网站。 我关注了this小教程,我陷入了疑问。
我不想要这个站点的任何数据库(无论是管理页面),所以我删除了 settings.py 中的数据库设置部分,然后我激活了“USE_I18N = True”和 ugettext 和其他一切。事实上,当我转到 localhost 时,它会正确显示两种语言的翻译,其中 /it/ 和 /en/ 放在 之后本地主机:8000.
我现在正在尝试制作一个用于切换语言的按钮,添加 djangoproject 代码到我的模板文件,在这里:

{% load i18n %} 
<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> 

问题是当我从下拉菜单中选择一种语言时,出现错误

ImproperlyConfigured at /it/i18n/setlang/ 
settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details. 
Request Method: POST 
Request URL:    http://localhost:8000/it/i18n/setlang/ 
Django Version: 1.8.2 
Exception Type: ImproperlyConfigured 
Exception Value:     
settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details. 

这是应用树:

sito_personale --- locale --- en --- LC_MESSAGES --- django.mo 
                                                 --- django.po 
                          --- it --- LC_MESSAGES --- django.mo 
                                                 --- django.po 
               --- pages --- migration 
                         --- static 
                         --- templates 
               --- sito_personale 
               --- manage.py 

我能做些什么来解决这个问题吗?

非常感谢您提供的任何帮助。

请您参考如下方法:

Django 将选择的语言保存到 session 中。 SESSION_ENGINE默认使用数据库,因为您不使用 db 这可能会导致您遇到异常。

尝试将 SESSION_ENGINE 设置为 django.contrib.sessions.backends.file,这会将 session 数据存储在磁盘上(参见 Using file-based sessions)。

所以在你的 settings.py 添加:

SESSION_ENGINE = 'django.contrib.sessions.backends.file' 

更新

如果您的网站不需要 session 支持,您也可以删除 session 中间件。在这种情况下,Django 将使用 cookie 来存储语言偏好

The view expects to be called via the POST method, with a language parameter set in request. If session support is enabled, the view saves the language choice in the user’s session. Otherwise, it saves the language choice in a cookie that is by default named django_language. (The name can be changed through the LANGUAGE_COOKIE_NAME setting.)


更新评论中的后续问题

根据 warning in the docs ,i18n 模式需要使用与语言无关的 urlpatterns:

Warning
Make sure that you don’t include the above URL within i18n_patterns() - it needs to be language-independent itself to work correctly.

这是一个简单的示例,说明如何从上面将其转换为您的 urls.py:

urlpatterns = solid_i18n_patterns('', 
    # Examples: 
    # url(r'^$', 'sito_personale.views.home', name='home'), 
    # url(r'^blog/', include('blog.urls')), 
    url(r'', include('pages.urls')), 
) 
 
urlpatterns += patterns('', 
    url(r'^i18n/', include('django.conf.urls.i18n')), 
)