【问题标题】:Can't solve django templatedoesnotexist error无法解决 django templatedoesnotexist 错误
【发布时间】:2021-11-22 03:11:40
【问题描述】:

我知道这个问题之前已经被问过很多次了,但我要么找不到过时的答案,要么通常有效。

即使我的服务器运行正常,我也会不断收到此错误

 Environment:


Request Method: GET
Request URL: http://localhost:8000/subscribe/

Django Version: 3.1.4
Python Version: 3.8.10
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'app',
 'subscribe']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Template loader postmortem
Django tried loading these templates, in this order:

Using engine django:
    * django.template.loaders.app_directories.Loader: /usr/local/lib/python3.8/dist-packages/django/contrib/admin/templates/subscribe/index.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: /usr/local/lib/python3.8/dist-packages/django/contrib/auth/templates/subscribe/index.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: /root/project/subscribe/templates/subscribe/index.html (Source does not exist)



Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/root/project/subscribe/views.py", line 16, in subscribe
    return render(request, 'subscribe/index.html', {'form':sub})
  File "/usr/local/lib/python3.8/dist-packages/django/shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/usr/local/lib/python3.8/dist-packages/django/template/loader.py", line 61, in render_to_string
    template = get_template(template_name, using=using)
  File "/usr/local/lib/python3.8/dist-packages/django/template/loader.py", line 19, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)

Exception Type: TemplateDoesNotExist at /subscribe/
Exception Value: subscribe/index.html

这些是我的模板设置

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],             
        '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',
            ],
        },
    },
]

我尝试更改 DIRS 并添加路径等,但没有任何变化。

我已将我所有的应用程序添加到 INSTALLED_APPS

编辑 1: 我的 /subscribe/views.py

from django.shortcuts import render
from project.settings import EMAIL_HOST_USER
from . import forms
from django.core.mail import send_mail

def subscribe(request) :
    sub = forms.Subscribe()
    if request.method == 'POST':
        sub = forms.Subscribe(request.POST)
        subject = 'Welcome to DataFlair'
        message = 'Hope you are enjoying your Django Tutorials'
        recepient = str(sub['Email'].value())
        send_mail(subject, 
            message, EMAIL_HOST_USER, [recepient], fail_silently = False)
        return render(request, 'subscribe/success.html', {'recepient': recepient})
    return render(request, 'subscribe/index.html', {'form':sub})

编辑 2

/app 中的模板正确显示,只有 /subscribe 中的模板有错误 有人有什么想法吗?

【问题讨论】:

  • 能否在您的应用订阅中显示您的views.py?
  • @amadousow 嗨!我刚刚编辑了它!
  • 如果对您有帮助,请接受以下答案。

标签: python python-3.x django


【解决方案1】:

尝试这样组织您的模板:我会假设订阅是您的应用程序。

subscribe/templates/subscribe/<put your html files here>

【讨论】:

  • 非常感谢!
  • @stelaa 我很高兴能够帮助你。快乐的编码)
【解决方案2】:

您需要在 settings.py 中提及您的模板目录路径

参考this

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],

DIRS 定义了引擎应该查找的目录列表 模板源文件,按搜索顺序排列。

【讨论】:

  • 不幸的是我已经尝试过了,但它什么也没做:/
  • @stelaa 你能显示你存储模板文件的位置
  • 我在项目图片中编辑了!
猜你喜欢
  • 2021-02-08
  • 2020-08-30
  • 1970-01-01
  • 2014-02-19
  • 2013-06-20
  • 2015-09-02
  • 2021-07-15
  • 2012-12-18
  • 1970-01-01
相关资源
最近更新 更多