Django 在我的 .envs 文件夹中查找模板(+ Django Admin)
Django looks for templates in my .envs folder (+ Django Admin)
我的文件夹设置如下:
~/projects/myproject/
~/.envs/myproject/
我的应用程序文件夹如下所示:
myapp/
├── __init__.py
├── admin.py
├── forms.py
├── migrations/
├── models.py
├── tests/
├── templates/
├── urls.py
├── views.py
我刚刚实现了自定义密码重置视图,因为我想使用自己的模板。问题是,密码重置视图找不到我的模板。这是我的观点:
class CustomPasswordResetConfirmView(views.PasswordResetConfirmView):
success_url = reverse_lazy("accounts:password_reset_complete")
template_name = "accounts/registration/password_reset_confirm.html"
这是我收到的错误消息:
TemplateDoesNotExist at /auth/reset/MQ/set-password/
accounts/registration/password_reset_confirm.html.
Request Method: GET
Request URL: http://localhost:8000/auth/reset/MQ/set-password/
Django Version: 1.11.13
Exception Type: TemplateDoesNotExist
Exception Value:
accounts/registration/password_reset_confirm.html.
Exception Location: /Users/jan/.envs/myproject/lib/python3.6/site-packages/django/template/loader.py in select_template, line 53
Python Executable: /Users/jan/.envs/myproject/bin/python
Python Version: 3.6.5
Python Path:
['/Users/jan/Dev/Misc Tutorial/myproject',
'/Users/jan/.envs/myproject/lib/python36.zip',
'/Users/jan/.envs/myproject/lib/python3.6',
'/Users/jan/.envs/myproject/lib/python3.6/lib-dynload',
'/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Users/jan/.envs/myproject/lib/python3.6/site-packages']
如何让 Django 找到我的自定义模板?从错误中我可以看出他在查看我的虚拟环境。
我试过像这样指定模板路径:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
[os.path.join(BASE_DIR, 'templates')]
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
我从 Django 文档中获得的代码。不幸的是,这没有帮助。如果您知道我该如何解决这个问题,我将不胜感激!
(在相关说明中,我还尝试使用 django-admin 而不是 manage.py,因为官方文档推荐它。但是我无法设置 DJANGO_SETTINGS_MODULE env 变量,以便它有效。也许这个问题是相关的。)
编辑:与帐户相关的应用称为帐户。其中有一个名为 templates 的文件夹,其中包含各种其他文件夹,其中一个名为 'registration'.
有点解决了。
按照 pydanny 的建议,使用 manage.py 绕过了 django-admin 问题。
而且我的模板配置有误。
这是我使用的新视图:
class CustomPasswordResetConfirmView(views.PasswordResetConfirmView):
success_url = reverse_lazy("accounts:password_reset_complete")
template_name = "registration/password_reset_confirm.html"
我也将模板文件夹放在我的项目根目录中,如下所示:
myproject
accounts/
...
myproject/
templates/
registration/
文件夹现在在此 templates/
目录中。
结合以下设置,现在一切正常,感谢所有帮助过的人!
设置:
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',
],
},
},
]
Django 也在我的 venv 目录(我的虚拟环境目录)中寻找模板,就我而言,我在主项目目录的设置文件中添加我的应用程序后解决了它。
基本上在您的应用程序目录中,有一个 apps.py
文件,其中有一个名为 class
*YOURAPPNAME*Config
。在我的例子中,应用程序是帐户,所以 class 是 AccountsConfig
。然后进入你的主项目目录,它与你的 manage.py
文件位于同一目录,打开 settings.py
,向下滚动到 INSTALLED_APPS
并在那里添加你的 class。对我来说是 accounts.apps.AccountsConfig
我的文件夹设置如下:
~/projects/myproject/
~/.envs/myproject/
我的应用程序文件夹如下所示:
myapp/
├── __init__.py
├── admin.py
├── forms.py
├── migrations/
├── models.py
├── tests/
├── templates/
├── urls.py
├── views.py
我刚刚实现了自定义密码重置视图,因为我想使用自己的模板。问题是,密码重置视图找不到我的模板。这是我的观点:
class CustomPasswordResetConfirmView(views.PasswordResetConfirmView):
success_url = reverse_lazy("accounts:password_reset_complete")
template_name = "accounts/registration/password_reset_confirm.html"
这是我收到的错误消息:
TemplateDoesNotExist at /auth/reset/MQ/set-password/
accounts/registration/password_reset_confirm.html.
Request Method: GET
Request URL: http://localhost:8000/auth/reset/MQ/set-password/
Django Version: 1.11.13
Exception Type: TemplateDoesNotExist
Exception Value:
accounts/registration/password_reset_confirm.html.
Exception Location: /Users/jan/.envs/myproject/lib/python3.6/site-packages/django/template/loader.py in select_template, line 53
Python Executable: /Users/jan/.envs/myproject/bin/python
Python Version: 3.6.5
Python Path:
['/Users/jan/Dev/Misc Tutorial/myproject',
'/Users/jan/.envs/myproject/lib/python36.zip',
'/Users/jan/.envs/myproject/lib/python3.6',
'/Users/jan/.envs/myproject/lib/python3.6/lib-dynload',
'/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Users/jan/.envs/myproject/lib/python3.6/site-packages']
如何让 Django 找到我的自定义模板?从错误中我可以看出他在查看我的虚拟环境。
我试过像这样指定模板路径:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
[os.path.join(BASE_DIR, 'templates')]
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
我从 Django 文档中获得的代码。不幸的是,这没有帮助。如果您知道我该如何解决这个问题,我将不胜感激!
(在相关说明中,我还尝试使用 django-admin 而不是 manage.py,因为官方文档推荐它。但是我无法设置 DJANGO_SETTINGS_MODULE env 变量,以便它有效。也许这个问题是相关的。)
编辑:与帐户相关的应用称为帐户。其中有一个名为 templates 的文件夹,其中包含各种其他文件夹,其中一个名为 'registration'.
有点解决了。
按照 pydanny 的建议,使用 manage.py 绕过了 django-admin 问题。
而且我的模板配置有误。 这是我使用的新视图:
class CustomPasswordResetConfirmView(views.PasswordResetConfirmView):
success_url = reverse_lazy("accounts:password_reset_complete")
template_name = "registration/password_reset_confirm.html"
我也将模板文件夹放在我的项目根目录中,如下所示:
myproject
accounts/
...
myproject/
templates/
registration/
文件夹现在在此 templates/
目录中。
结合以下设置,现在一切正常,感谢所有帮助过的人!
设置:
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',
],
},
},
]
Django 也在我的 venv 目录(我的虚拟环境目录)中寻找模板,就我而言,我在主项目目录的设置文件中添加我的应用程序后解决了它。
基本上在您的应用程序目录中,有一个 apps.py
文件,其中有一个名为 class
*YOURAPPNAME*Config
。在我的例子中,应用程序是帐户,所以 class 是 AccountsConfig
。然后进入你的主项目目录,它与你的 manage.py
文件位于同一目录,打开 settings.py
,向下滚动到 INSTALLED_APPS
并在那里添加你的 class。对我来说是 accounts.apps.AccountsConfig