我知道此网站上有关于此问题的多个问题,但我找不到解决方案。
我在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
答案 0 :(得分:2)
在你的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/
答案 1 :(得分:1)
将DIRS
下的TEMPLATES
更改为此值,告诉django在您的应用目录中查找模板。
'DIRS': [os.path.join(BASE_DIR, 'templates'),],
然后您可以将模板与 manage.py 平行放置,也可以放在特定的应用目录下。
+app
-+__init__.py
-+templates
---+polls
-----+index.html
答案 2 :(得分:1)
在您的设置中更改已安装应用的订单
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls.apps.PollsConfig',
]
答案 3 :(得分:0)
您需要做的就是将您的应用名称(即民意调查)添加到settings.py文件中已安装应用的列表中,然后将URL重新加载到应用索引页面。我不知道为什么Django教程仍然不包含此步骤,因为它每次都会使每个人绊倒。
settings.py
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
]