简体   繁体   中英

Why this form doesn't change the language of the website?

This is my language-switching form.

{% get_current_language as lang_code %}
{% get_available_languages as languages %}
{% get_language_info_list for languages as langs %}
<form action="{% url 'set_language' %}" method="POST">
{% csrf_token %}
<select name="language" title="{% translate 'Language' %}">
{% for lang in langs %}
<option value="{{ lang.code }}"{% if lang.code == lang_code %} selected{% endif %}>
{{ lang.name_local }} ({{ lang.name_translated }})
</option>
{% endfor %}
</select>
<input type="submit" value="OK" />
</form>

I include this in the footer of my base template.

Then, here is my courses/urls.py (app urls).

from django.contrib import admin
from django.urls import path
from django.utils.translation import gettext_lazy as _
from . import views

admin.site.site_header = _("FilFak administration")
admin.site.site_title = _("FilFak admin")
admin.site.index_title = _("Manage FilFak")

urlpatterns=[
    path("courses/", views.CourseList.as_view(), name="course_list"),
    path("professors/", views.ProfessorList.as_view(), name="professor_list"),
    path("exams/", views.ExamList.as_view(), name="exam_list"),
    path("courses/<slug:slug>", views.CourseDetails.as_view(), name="course_details"),
    path("professors/<slug:slug>", views.ProfessorDetails.as_view(), name="professor_details"),
    path("exams/<slug:slug>", views.ExamDetails.as_view(), name="exam_details"),
    path("", views.Index.as_view(), name="index"),
]

filfak/urls.py (project urls.py)

from django.contrib import admin
from django.urls import path, include
from django.conf.urls.i18n import i18n_patterns
from django.conf.urls.static import static
from django.conf import settings
from django.utils.translation import gettext_lazy as _

urlpatterns = [
    path("admin/", admin.site.urls),
    path("lang/", include("django.conf.urls.i18n")),
]

urlpatterns += i18n_patterns(
    path("", include("courses.urls")),
    prefix_default_language=False
)

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

And here's the list of middlewares from settings.py.

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.locale.LocaleMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

When I click OK on the language-switching form, it actually changes the language only on the homepage of the default language. On any other URL it just refreshes the page, without changing the language.

Am I doing something wrong?

Check the flag value of USE_I18N , look like its set to False and thats why the language are not setting properly.

Change it to :-

USE_I18N = True

As per Django document :-

If you don't use internationalization, you should take the two seconds to set USE_I18N = False in your settings file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM