gpt4 book ai didi

python - Django 中的模板不存在

转载 作者:行者123 更新时间:2023-11-30 23:12:24 27 4
gpt4 key购买 nike

你好,我是 django 的新手,我遇到了这个 TemplateDoesNotExist 错误

我运行的是 Windows 8

当我检查错误页面时,值 TEMPLATE_DIRS 是 ['C:/ENVS/boardgames/boardgames/templates'] 并且此路径完全正确。从那里我需要加载“helloworld.html”作为模板

我在Views.py中这样做

from django.views.generic.base import TemplateView
from django.http import HttpResponse


class HelloWorldView(TemplateView):
template_name='helloworld.html'

Settings.py

"""
Django settings for boardgames project.

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

"""

# 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__)))



# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*r7$d%0eg(^7e&-ad)%(17zv-zm-3m87uroxe+9+e)4&qg4w(a'

# 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',
'boardgames',
)

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 = 'boardgames.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]

WSGI_APPLICATION = 'boardgames.wsgi.application'



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


# Internationalization


LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)


STATIC_URL = '/static/'
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates').replace('\\','/')]

Url.py

from django.conf.urls import include, url
from django.contrib import admin

from .views import HelloWorldView
urlpatterns = [
# Examples:
url(r'^$', HelloWorldView.as_view(), name='home'),
# url(r'^blog/', include('blog.urls')),

url(r'^admin/', include(admin.site.urls)),
]

当我查看教程时,它看起来很简单。当我将 templates 文件夹复制到 django\contrib 目录时,它实际上起作用了。我真的很痛苦。但我希望正确完成并从我希望它们所在的目录加载模板

我做错了什么?请帮忙

最佳答案

TEMPLATE_DIRS 在 django 1.8 中已弃用。您应该改用TEMPLATES 设置。您的 settings.py 文件中已包含此变量,因此请按如下所示更改它:

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates').replace('\\','/')],
'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',
],
},
},
]

关于python - Django 中的模板不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29844023/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com