pycharm 没有名为'django.templates'的模块

ubbxdtey  于 2022-11-08  发布在  PyCharm
关注(0)|答案(7)|浏览(845)

我正在使用python 3.4,django 1.8。我正在使用pycharm进行开发。当运行程序时,我得到了“没有名为”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>

网站名称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
vxbzzdmp

vxbzzdmp1#

如果你不熟悉Pycharm IDE重构,可能会发生这种情况。你通过重构将“templates”目录重命名为另一个名称。当你这样做时,Pycharm也会重命名“www.example.com“中的一些字符串settings.py。所以右键单击“setting.py“-〉LocalHistory -〉ShowHistory,然后备份“setting.py”。

dvtswwa3

dvtswwa32#

用这个替换现有的值。就像在上一篇文章中他告诉你的那样,你只需要从相关的字段中删除“s”。但是如果你不理解它,那么就复制粘贴。

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

        },
    },
]
zkure5ic

zkure5ic3#

正如错误所提到的,没有django.templates这样的东西。
但是,有一个django.template,您需要从所有引用templates的相关行中删除s
当你这样做的时候,你也应该把你的TEMPLATE_LOADERS移到它们合适的位置(在TEMPLATES设置内)。
阅读更多文档。

zbdgwd5y

zbdgwd5y4#

当我开始我的第一个django项目的时候,我得到了和你一样的例外...我想也许你在pycharm中把模板目录名从'template'改成了'templates',但是没有意识到pycharm在同一时间改变了www.example.com中的TEMPLATES.context_processors配置setting.py。它改变了

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

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

.这就是问题的原因。

dgenwo3n

dgenwo3n5#

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

xbp102n0

xbp102n06#

在settings.py模板列表中有一个空的键名DIRS,它不应该是**'DIRS':[],正确的方法-〉在settings.py中添加该值'DIRS':[BASE_DIR / 'templates']**

ctrmrzij

ctrmrzij7#

真的,在我的例子中,它与从模板到模板的目录名重构有关。需要在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

相关问题