2017-09-14 223 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