模板 Django(不存在于/)

Templates Django (Does not exist at/)

我正在尝试使用 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

出于想法,我需要你的意见。 感谢您的宝贵时间

TEMPLATE_DIRS 设置在 django 1.8 中已弃用。您应该改用 TEMPLATES

TEMPLATES 变量存在于默认 settings.py 文件中,因此找到它并更改 'DIRS' 键:

TEMPLATES = [
    {
        ...
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        ...
    },
]

右括号有错字。所以语法错误

使用这个

'DIRS': [os.path.join(BASE_DIR, 'templates')],

P.S:请编辑您的问题,而不是添加问题的答案:P

出现错误,

从您的目录结构来看,您有两个模板目录,一个在主项目中,另一个在 mysite/polls/mysite/ 的投票应用程序中。 Django 正在项目模板中寻找模板,即

/home/florian/Documents/mysite/templates/mysite/bap2pmonitoring.html

但是你的模板在

/home/florian/Documents/mysite/polls/templates/mysite/bap2pmonitoring.html

将您的模板放在主项目模板目录中,它应该可以工作。

您未能在

中添加您的应用

INSTALLED_APPS = [ 'mysite', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ]

在 settings.py.