首页 > 解决方案 > TemplateDoesNotExist 位于 /music/1/favourite/music/details.html

问题描述

我是 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>

标签: python-3.xdjango-templatesdjango-viewsdjango-2.0

解决方案


检查设置中的模板文件夹,在这部分错误中您可以看到 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


推荐阅读