Templates Django (Does not exist at/)

Deadly 提交于 2019-12-25 03:34:31

问题


I'm trying to use the Django templates but i cant even display a simple html line i don't understand why... I searched to resolve it but after many test, the problem remains.

This is my structure:

My urls.py (which work i tried it with another fonction in 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)),
)

My 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')

My setting file 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',
            ],
        },
    },
]

My simple html doc:

<h1> BAP2P Monitoring </h1>

And the output finally my main problem :

And this is the traceback:

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

Out of ideas, i need your opinion please. Thanks for your time


回答1:


P.S: Please edit your question instead of adding an answer to your question with a question :P

Coming to the error,

From your directory structure, you have two template directories, one in the main project and other in the polls app at mysite/polls/mysite/. Django is looking for templates in the project templates i.e.,

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

but you have your template in

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

Put your templates in the main project template directory and it should work.




回答2:


TEMPLATE_DIRS setting is deprecated in django 1.8. You should use the TEMPLATES instead.

The TEMPLATES variable is exists in the default settings.py file so find it and alter the 'DIRS' key:

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



回答3:


It has a typo with the closing bracket. so the syntax error

use this

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




回答4:


you are missing to add your app in

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

in settings.py.



来源:https://stackoverflow.com/questions/30188635/templates-django-does-not-exist-at

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!