2017-09-14 228 views
2

使用Django版本1.9.5和Python 3Django的模板模块导入错误

在导航到url,我收到以下错误:

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

错误似乎是从未来进口线。检查语法并尝试在DIRS的TEMPLATES下明确提供路径,但结果相同。任何人遇到类似的问题?发现了一些类似的问题,但用不同的语言。

模板文件夹结构:name_of_app /模板/ 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', 
      ], 
     }, 
    }, 
] 

回答

3

这看起来像程序员的最大克星:一个错字。您可以在第二段代码片段中看到对django.templates...的引用,但您的第二行从django.template导入。

编辑:从我的测试中,不正确的导入将在shell中失败,并且context_processors中的错误引用将在浏览器中失败。

+0

试过了。服务器甚至没有启动跟踪错误。相当肯定我的当前导入语句基于IDE语法突出显示是正确的,因为如果添加s,加载程序无法识别。 - ImportError:没有名为'django.templates'的模块 – user3050832

+0

我更新了我的答案。错字是在你的'context_processors'中,而不是在导入中。 – Franey

+0

你的意思是在settings.py文件下,更改为django.template.context_processors.request? (删除s)。如果是的话,得到相同的错误。 – user3050832

0

尝试使用这种方法使用模块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) 
1
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', 
      ], 
     }, 
    }, 
] 
+0

用上面的代码替换settings.py中现有的TEMPLATES结构。 –

+0

考虑添加关于您的答案如何工作的简要说明。您还可以编辑您的答案以添加更多有价值的信息 – crgarridos