简体   繁体   中英

Django template Module Import Error

Using Django version 1.9.5 and Python 3.

Upon navigation to the url, I am getting the following error:

ImportError at /music/
No module named 'django.templates'
Request Method: GET
Request URL:    http://localhost:8000/music/
Django Version: 1.9.5
Exception Type: ImportError
Exception Value:    
No module named 'django.templates'
Exception Location: D:\Program Files (x86)\Python\lib\importlib\__init__.py in import_module, line 126
Python Executable:  D:\Program Files (x86)\Python\python.exe
Python Version: 3.5.0
Python Path:    
['C:\\Users\\ang\\Desktop\\website',
 'D:\\Program Files (x86)\\Python\\lib\\site-packages\\django-1.9.5-py3.5.egg',
 'D:\\Program Files (x86)\\Python\\python35.zip',
 'D:\\Program Files (x86)\\Python\\DLLs',
 'D:\\Program Files (x86)\\Python\\lib',
 'D:\\Program Files (x86)\\Python',
 'D:\\Program Files (x86)\\Python\\lib\\site-packages']

The error seems to be coming from the import line. Checked syntax and tried to explicitly provided the path under TEMPLATES at DIRS but same result. Anyone encountered a similar issue? Found some similar issues raised but in different languages.

Folder Structure for template: name_of_app/templates/inner_folder/html_file

music/templates/music/index.html

views.py

from django.http import HttpResponse
from django.template import loader # error gone if i remove this line
from .models import Album


def index(request):
    all_albums = Album.objects.all()
    template = loader.get_template('music/index.html')
    context = {
        'all_albums': all_albums
    }
    return HttpResponse('test test') 

settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.templates.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.templates.context_processors.debug',
                'django.templates.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

This looks like the programmer's greatest nemesis: a typo. You can see midway in the 2nd code snippet a reference to django.templates... , but your 2nd line is importing from django.template .

Edit: From my testing, an incorrect import will fail in the shell, and an incorrect reference in context_processors will fail in the browser.

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

Try to use this method to render your template using module django.shortcurt.render() :

from django.shortcuts import render
from .models import Album


def index(request):
    all_albums = Album.objects.all()
    context = {
        'all_albums': all_albums
    }
    return render(request, 'music/index.html',context=context) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM