Django 无法识别 template_name

问题描述 投票:0回答:1

设置.py

INSTALLED_APPS = [
    ...,
    'authentication_system_app',
]

TEMPLATES = [
    {
        ...,
        'DIRS': ['authentication_system_app/templates/'],
        ...,
    },
]

authentication_system_app.urls

from django.contrib.auth import views as auth_views

path('account/reset-password/', auth_views.PasswordChangeView.as_view(template_name='reset_password.html'), name='reset_password'),

我不知道为什么

template_name
在我的宠物项目上不起作用,但是当我观看视频教程时,它效果很好。

我不知道为什么,你们能帮帮我吗?太疯狂了!

GitHub代码:身份验证系统

python django django-views django-templates
1个回答
0
投票

在您的根目录中创建一个名为

templates
的文件夹(与您的 manage.py 文件位于同一位置)。然后,确保
TEMPLATES
中的
settings.py
列表如下所示:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

如果您更喜欢@FlipperPA在评论中建议的方法,那么在您的

TEMPLATES
列表中,使用
"DIRS": []
"APP_DIRS": True
。这意味着您必须在应用程序目录中创建一个
templates
文件夹,然后在该模板文件夹中创建一个以应用程序名称命名的子文件夹,并且您的 html 文件应位于该子文件夹中。这很令人困惑,但基本上,就你的情况而言,它应该看起来像这样
authentication_system_app/templates/authentication_system_app/reset_password.html

© www.soinside.com 2019 - 2024. All rights reserved.