我试图使用Django模板,但我甚至无法显示简单的html行,我不明白为什么...... 我搜索解决了它,但经过多次测试后,问题仍然存在。
这是我的结构:
我的urls.py(我尝试使用views.py中的另一个功能):
from django.conf.urls import patterns,include, url
from django.contrib import admin
urlpatterns = patterns('polls.views',
url(r'^$', 'home', name = 'home'),
url(r'^admin/', include(admin.site.urls)),
)
我的views.py:
from django.shortcuts import render
from django.template import Template , Context
# Create your views here.
# -*- coding: utf-8 -*-
def home(request):
return render(request, 'mysite/bap2pmonitoring.html')
我的设置文件setting.py:
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',
],
},
},
]
我简单的HTML文档:
<h1> BAP2P Monitoring </h1>
输出最后是我的主要问题:
这是追溯:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.8.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles')
Installed Middleware:
('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')
Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/mysite/bap2pmonitoring.html (File does not exist)
/usr/local/lib/python2.7/dist-packages/django/contrib/auth/templates/mysite/bap2pmonitoring.html (File does not exist)
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/florian/Documents/mysite/polls/views.py" in home
8. return render(request, 'mysite/bap2pmonitoring.html')
File "/usr/local/lib/python2.7/dist-packages/django/shortcuts.py" in render
67. template_name, context, request=request, using=using)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in render_to_string
98. template = get_template(template_name, using=using)
File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py" in get_template
46. raise TemplateDoesNotExist(template_name)
Exception Type: TemplateDoesNotExist at /
Exception Value: mysite/bap2pmonitoring.html
出于想法,我需要你的意见。 谢谢你的时间
答案 0 :(得分:6)
TEMPLATE_DIRS
设置。您应该使用TEMPLATES
代替。
TEMPLATES
变量存在于默认的settings.py
文件中,因此找到它并更改'DIRS'
密钥:
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
...
},
]
答案 1 :(得分:4)
P.S:请编辑您的问题,而不是在问题中添加问题答案:P
出现错误,
从您的目录结构中,您有两个模板目录,一个位于主项目中,另一个位于民意调查应用中 mysite / polls / mysite / 。 Django正在寻找项目模板中的模板,即
/home/florian/Documents/mysite/templates/mysite/bap2pmonitoring.html
但是你的模板在
中/home/florian/Documents/mysite/polls/templates/mysite/bap2pmonitoring.html
将模板放在主项目模板目录中,它应该可以工作。
答案 2 :(得分:2)
它有一个拼写错误的结束括号。所以语法错误
使用此
'DIRS':[os.path.join(BASE_DIR,'templates')],
答案 3 :(得分:0)
您缺少在
中添加应用的信息INSTALLED_APPS = [ “ mysite”, 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ]
在settings.py中。