TemplateDoesNotExist 位于 /music/1/favourite/music/details.html

Posted

技术标签:

【中文标题】TemplateDoesNotExist 位于 /music/1/favourite/music/details.html【英文标题】:TemplateDoesNotExist at /music/1/favourite/ music/details.html 【发布时间】:2019-01-01 06:42:35 【问题描述】:

我是 Django 新手,我使用的是 2.0.7 版。我已经创建了名为 details.html 的模板,但它仍然没有显示

我指的视频教程是https://www.youtube.com/watch?v=qgGIqRFvFFk&list=PL6gx4Cwl9DGBlmzzFcLgDhKTTfNLfX1IK&index=24

我得到的错误是这样的

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/music/1/favourite/

Django Version: 2.0.7
Python Version: 3.6.6
Installed Applications:
['music.apps.MusicConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
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: C:\Users\Adesh\Desktop\website\music\templates\music\details.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\admin\templates\music\details.html (Source does not exist)
    * django.template.loaders.app_directories.Loader: C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\templates\music\details.html (Source does not exist)



Traceback:

File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\datastructures.py" in __getitem__
  77.             list_ = super().__getitem__(key)

During handling of the above exception ('song'), another exception occurred:

File "C:\Users\Adesh\Desktop\website\music\views.py" in favourite
  22.         selected_song = album.song_set.get(pk=request.POST['song'])

File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\datastructures.py" in __getitem__
  79.             raise MultiValueDictKeyError(key)

During handling of the above exception ('song'), another exception occurred:

File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\Adesh\Desktop\website\music\views.py" in favourite
  26.             'error_message':"You did not select a valid song",

File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-packages\django\shortcuts.py" in render
  36.     content = loader.render_to_string(template_name, context, request, using=using)

File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\loader.py" in render_to_string
  61.         template = get_template(template_name, using=using)

File "C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\loader.py" in get_template
  19.     raise TemplateDoesNotExist(template_name, chain=chain)

Exception Type: TemplateDoesNotExist at /music/1/favourite/
Exception Value: music/details.html

我的模板如下命名为 details.html

<img src=" album.album_logo">

<h1> album.album_title </h1>
<h2> album.artist </h2>

% if error_message %
    <p><strong> error_message </strong></p>
% endif %

<form action="% url 'music:favourite' album.id %" method="post" >
    % csrf_token %
    % for song in album.song_set.all %
        <input type="radio" id="song forloop.counter " name="song" 
value=" song.id ">
        <label for="song  forloop.counter ">
             song.song_title 
            % if song.is_favourite %
                <img src="random_image.png"/>
            % endif %
        </label><br>
    % endfor %
    <input tye="submit" value="Favourite">
</form>

【问题讨论】:

【参考方案1】:

在设置中检查你的模板文件夹,在这部分错误中你可以看到 Django 找不到模板:

Using engine django:
* django.template.loaders.app_directories.Loader: C:\Users\Adesh\Desktop\website\music\templates\music\details.html (Source does not exist)
* django.template.loaders.app_directories.Loader: C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\admin\templates\music\details.html (Source does not exist)
* django.template.loaders.app_directories.Loader: C:\Users\Adesh\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\templates\music\details.html (Source does not exist)

这些是 Django 尝试在其中查找模板的目录。Django 查找的目录在项目的 settings.py 文件中指定,此变量:TEMPLATE_DIRS 或 TEMPLATES 具有 Django 查找模板的路径。

要么将模板移动到这些文件夹之一,要么将保存模板的文件夹添加到此变量。

另请参阅 Django 文档中有关模板的这一部分: https://docs.djangoproject.com/en/2.0/topics/templates/#django.template.backends.base.Template.render

【讨论】:

【参考方案2】:

尝试在 settings.py 文件中添加“模板”目录的路径。您可以检查下面的代码块;我在 TEMPLATES 部分将模板目录的完整路径添加到“DIRS”,它解决了我的问题。

TEMPLATES = [

    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [r'C:\Users\Exper\PycharmProjects\Django\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',
        ],
    ,
,

]

【讨论】:

以上是关于TemplateDoesNotExist 位于 /music/1/favourite/music/details.html的主要内容,如果未能解决你的问题,请参考以下文章

TemplateDoesNotExist 位于 /music/1/favourite/music/details.html

TemplateDoesNotExist at / Home.html 异常类型:TemplateDoesNotExist

TemplateDoesNotExist 异常

Django 管理站点:TemplateDoesNotExist?

TemplateDoesNotExist - Django 错误

在 / 处获取 TemplateDoesNotExist