我知道此站点上对此问题有多个问题,但是我找不到解决方案。
我正在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)
我在文件夹
index.html
中创建了文件polls\templates\polls
我的
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/