gpt4 book ai didi

python - django2 + python3 : TemplateDoesNotExist

转载 作者:太空宇宙 更新时间:2023-11-04 09:48:40 26 4
gpt4 key购买 nike

我知道这个网站上有很多关于这个问题的问题,但我找不到解决方案。

我在 Windows 10 上使用 Python 3.6 (anaconda) + django 2.0.2。

我正在学习教程:https://docs.djangoproject.com/en/2.0/intro/tutorial03/

这是我的views.py

from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse

from .models import *


def index(request):
content = 'abcxyz'

context = {'content': content}
return render(request, 'polls/index.html', context)

我在文件夹 polls\templates\polls 中创建了一个文件 index.html

我的settings.py:

INSTALLED_APPS = [
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'django_site.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 = 'django_site.wsgi.application'


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

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

我有一个“TemplateDoesNotExist”的问题——在我看来 django 试图在

django.template.loaders.app_directories.Loader: /mnt/e/workspace/capec-processing/code/django_site/polls/templates/polls/templates/polls/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/user_name/anaconda3/envs/capec/lib/python3.6/site-packages/django/contrib/admin/templates/polls/templates/polls/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/user_name/anaconda3/envs/capec/lib/python3.6/site-packages/django/contrib/auth/templates/polls/templates/polls/index.html (Source does not exist)

我不确定我做错了什么,因为我按照django网站上的教程。

你能给我一个提示吗?

更新

这是我的根目录(称为django_site)的结构:

django_site
--django_site
----settings.py
--polls
----templates
------polls
--------index.html
----views.py
--db.sqlite3
--manage.py

最佳答案

在你的setting.py中你需要添加这个

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls' # You need to add this too. This should be same as your app name.
]

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "templates")], # Add this to your settings file
'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',
],
},
},
]

你的模板文件夹应该在这里

myproject/
|-- myproject/
| |-- polls/
| |-- myproject/
| |-- templates/ <-- here!
| | |-- polls/
| | | |-- index.html
| | |-- base.html
| | +-- home.html
| +-- manage.py
+-- venv/

关于python - django2 + python3 : TemplateDoesNotExist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48804194/

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