How can I change Django admin language?

匆匆过客 提交于 2019-12-06 20:29:28

问题


I have a django 1.6 site with i18n working. I can change the frontend language with a select box in the top of the template, but I don't know if there is a django app or trick to change the admin language, because it seems to store somewhere in session variable, and it keeps the first language I have used in the frontend.


回答1:


You can create /en/admin, /fr/admin/ and so on using i18n_patterns:

urlpatterns += i18n_patterns(
    url(r'^admin/', include(admin.site.urls)),
)

(For Django <= 1.7, you must specify a prefix, use i18n_patterns('', ... ))




回答2:


In your settings.py just add 'django.middleware.locale.LocaleMiddleware' to your MIDDLEWARE_CLASSES setting, making sure it appears after 'django.contrib.sessions.middleware.SessionMiddleware'.




回答3:


Here is a slightly modified version of a code snippet from the Django docs for admin/base.html that adds a language selection dropdown:

{% block userlinks %}
{{ block.super }}
/ <form action="{% url 'set_language' %}" method="post" style="display:inline">{% csrf_token %}
    <input name="next" type="hidden" value="{{ redirect_to }}">
    <select name="language" onchange="this.form.submit()">
        {% 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{% endif %}>
                {{ language.name_local }} ({{ language.code }})
            </option>
        {% endfor %}
    </select>
</form>
{% endblock %}

For this to work you also need to add the following to your urlpatterns:

path('i18n/', include('django.conf.urls.i18n')),


来源:https://stackoverflow.com/questions/21469470/how-can-i-change-django-admin-language

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