使用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',
],
},
},
]
試過了。服務器甚至沒有啓動跟蹤錯誤。相當肯定我的當前導入語句基於IDE語法突出顯示是正確的,因爲如果添加s,加載程序無法識別。 - ImportError:沒有名爲'django.templates'的模塊 – user3050832
我更新了我的答案。錯字是在你的'context_processors'中,而不是在導入中。 – Franey
你的意思是在settings.py文件下,更改爲django.template.context_processors.request? (刪除s)。如果是的話,得到相同的錯誤。 – user3050832