模板Django(不存在于/)

Bou*_*h10 0 python django templates views django-templates

我正在尝试使用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)),
)
Run Code Online (Sandbox Code Playgroud)

我的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')
Run Code Online (Sandbox Code Playgroud)

我的设置文件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',
            ],
        },
    },
]
Run Code Online (Sandbox Code Playgroud)

我简单的HTML文档:

<h1> BAP2P Monitoring </h1>
Run Code Online (Sandbox Code Playgroud)

输出最后是我的主要问题:

在此输入图像描述

这是追溯:

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
Run Code Online (Sandbox Code Playgroud)

出于想法,我需要你的意见.谢谢你的时间

cat*_*ran 7

TEMPLATE_DIRSdjango 1.8中不推荐使用该设置.你应该使用TEMPLATES相反的.

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

TEMPLATES = [
    {
        ...
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        ...
    },
]
Run Code Online (Sandbox Code Playgroud)


小智 5

PS:请编辑您的问题,而不要在问题中添加答案:P

出现错误,

在目录结构中,您有两个模板目录,一个在主项目中,另一个在polls应用程序中,位于mysite / polls / mysite /。Django正在项目模板中寻找模板,例如,

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

但是你有你的模板

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

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