切换语言时 /i18n/setlang/ 处的 OperationalError
OperationalError at /i18n/setlang/ when switching language
我是 python 和 django 的新手,我正在尝试使用 2 种语言设置我的网站。当我使用 base.html 中的表单切换语言时,我收到此
OperationalError at /i18n/setlang/
no such table: django_session
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',
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LOCALE_PATHS = [
os.path.join(BASE_DIR, 'locale')
]
LANGUAGES = [
('it', ('Italian')),
('en', ('English')),
]
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.i18n',
)
urls.py
urlpatterns =[
path('i18n/', include('django.conf.urls.i18n')),
path('admin/', admin.site.urls),
path('', include('index.urls')),
path('about-us/', include('about.urls')),
]
base.html
<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{% endif %}>
{{ language.name_local }} ({{ language.code }})
</option>
{% endfor %}
</select>
<input type="submit" value="Go" />
</form>
感谢建议
我找到了解决办法
python manage.py makemigrations
python manage.py migrate
我是 python 和 django 的新手,我正在尝试使用 2 种语言设置我的网站。当我使用 base.html 中的表单切换语言时,我收到此
OperationalError at /i18n/setlang/ no such table: django_session
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',
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LOCALE_PATHS = [
os.path.join(BASE_DIR, 'locale')
]
LANGUAGES = [
('it', ('Italian')),
('en', ('English')),
]
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.i18n',
)
urls.py
urlpatterns =[
path('i18n/', include('django.conf.urls.i18n')),
path('admin/', admin.site.urls),
path('', include('index.urls')),
path('about-us/', include('about.urls')),
]
base.html
<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{% endif %}>
{{ language.name_local }} ({{ language.code }})
</option>
{% endfor %}
</select>
<input type="submit" value="Go" />
</form>
感谢建议
我找到了解决办法
python manage.py makemigrations
python manage.py migrate