0

I am new to Django and i am using it's version 2.0.7. I have already created template named as details.html but it's still not showing

The video tutorial which i am referring is https://www.youtube.com/watch?v=qgGIqRFvFFk&list=PL6gx4Cwl9DGBlmzzFcLgDhKTTfNLfX1IK&index=24

The error which I got is this

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

My template is as follows named as 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>

2 Answers2

0

Check your template folders in the settings, in this part of the error you can see Django can't find the template:

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)

These are the directories that Django tried to find the template in. The directories that Django looks for are specified in the settings.py file of your project, either this variable: TEMPLATE_DIRS or TEMPLATES has the paths that Django looks for templates.

Either move the template to one of these folders or add the folder where you've saved the template to this variable.

Also see this part of the Django documentation about templates: https://docs.djangoproject.com/en/2.0/topics/templates/#django.template.backends.base.Template.render

0

Try to add the path of your "Template" directory on settings.py file. You can check the code block below; I added full path of my template directory to 'DIRS' in TEMPLATES part and it solved my problem.

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',
        ],
    },
},

]

Johnny
  • 459
  • 1
  • 4
  • 12