没有名为' django.templates'

时间:2015-05-15 12:53:34

标签: django python-3.x django-templates pycharm

我正在使用python 3.4,django 1.8。我正在使用pycharm进行开发。在运行该程序时,我得到了“没有名为&d; django.templates'''错误。

settings.py

"""
Django settings for dj project.

Generated by 'django-admin startproject' using Django 1.8.1.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'uhqkhi7h_w48bz*gnr+_!roaa8@c_)087a(!ees)mn2=n=lv-r'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'dj.urls'

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


TEMPLATE_LOADERS = (
    'django.template.loaders.app_directories.load_template_source',
)
WSGI_APPLICATION = 'dj.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR,  'templates'),
)

博客/ urls.py

from django.conf.urls import include, url
from . import views

urlpatterns = [
url(r'^$', views.post_list),
]

manage.py

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dj.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

post_list.html

<html>
<body>
<p>
    hihiiii
</p>
</body>

</html>

DJ / urls.py

from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include('blog.urls')),
]

错误

ImportError at /
No module named 'django.templates'
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.8.1
Exception Type: ImportError
Exception Value:    
No module named 'django.templates'
Exception Location: C:\Python34\lib\importlib\__init__.py in import_module, line 109
Python Executable:  C:\Python34\python.exe
Python Version: 3.4.1
Python Path:    
['C:\\Users\\ankur anand\\PycharmProjects\\dj',
 'C:\\Users\\ankur anand\\PycharmProjects\\dj',
 'C:\\Windows\\SYSTEM32\\python34.zip',
 'C:\\Python34\\DLLs',
 'C:\\Python34\\lib',
 'C:\\Python34',
 'C:\\Python34\\lib\\site-packages']
Server time:    Fri, 15 May 2015 18:08:59 +0530

6 个答案:

答案 0 :(得分:7)

使用此选项并替换现有值。正如在上一篇文章中他告诉你的那样,你只需要删除&#34; s&#34;来自相关领域。但如果您无法理解它,那么只需复制粘贴。

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, "templates")],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],

        },
    },
]

答案 1 :(得分:7)

第一次使用Pycharm IDE的新手总会遇到此错误。 您通过重构将“templates”目录重命名为另一个名称。当你这样做时,它也在“settings.py”中重命名了一些字符串。 所以右键单击“setting.py” - &gt; LocalHistory - &gt; ShowHistory然后备份你“setting.py”。

答案 2 :(得分:5)

正如错误所提到的,没有django.templates这样的东西。

但是, django.template。您需要从引用s的所有相关行中删除templates

当您接触它时,您还应将TEMPLATE_LOADERS移至正确的位置(TEMPLATES设置内)。

了解更多in the docs

答案 3 :(得分:1)

在开始我的第一个django项目时,我遇到了同样的例外...... 我想也许你改变了模板的名称来自&#39;模板&#39;到&#39;模板&#39;在pycharm中,但没有意识到pycharm同时在settings.py中更改了TEMPLATES.context_processors配置。它改变了

django.template.context_processors.debug 
django.template.context_processors.request

django.templates.context_processors.debug 
django.templates.context_processors.request

。这是导致问题的原因。

答案 4 :(得分:0)

是的,在我的情况下,这与将目录名称从模板重构为模板有关。需要在TEMPLATES下更正更改settings.py文件(对我而言,错误是未找到模块“ django.template”) 在TEMPLATES下进行的更改是从中删除“ s”

django.templates.context_processors.debug

django.templates.context_processors.request

收件人:

django.template.context_processors.debug

django.template.context_processors.request

答案 5 :(得分:0)

这确实是由于 Pycharm 中的重构而发生的。当将目录名称“模板”更改为“模板”时,我最初遇到了同样的问题。 要解决此问题:右键单击 settings.py > 本地历史记录 > 显示历史记录 > 还原。 由于重构,它恢复了在设置文件中所做的更改,但将目录名称保留为“模板”。因此,解决了问题。