【问题标题】:Django template Module Import ErrorDjango模板模块导入错误
【发布时间】:2017-09-14 23:16:06
【问题描述】:

使用 Django 版本 1.9.5 和 Python 3。

在导航到 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/templates/inner_folder/html_file

音乐/模板/音乐/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',
            ],
        },
    },
]

【问题讨论】:

    标签: python django


    【解决方案1】:

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

    编辑:根据我的测试,错误的导入将在 shell 中失败,context_processors 中的错误引用将在浏览器中失败。

    【讨论】:

    • 试过了。服务器甚至没有启动跟随错误。根据 IDE 语法高亮显示,我当前的 import 语句是正确的,因为如果我添加 s,则无法识别加载程序。 - ImportError:没有名为“django.templates”的模块
    • 我更新了我的答案。错字在您的context_processors 中,而不是在导入中。
    • 你的意思是在settings.py文件下,改成django.template.context_processors.request? (删除 s)。如果是,得到同样的错误。
    • @user3050832 我在您的TEMPLATES 设置中看到三个对django.templates 的引用。尝试更新所有这三个,看看是否有帮助。
    • 有效!谢谢。
    【解决方案2】:
    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',
                ],
            },
        },
    ]
    

    【讨论】:

    • 用上面的代码替换你现有的settings.py中的TEMPLATES结构。
    • 考虑添加关于您的答案如何工作的简要说明。您还可以编辑您的答案以添加更多有价值的信息
    【解决方案3】:

    尝试使用此方法通过模块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) 
    

    【讨论】:

      猜你喜欢
      • 2015-03-23
      • 2012-02-28
      • 2017-01-15
      • 2015-02-09
      • 2017-03-13
      • 2012-10-26
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多