Template does not exist

隐身守侯 提交于 2019-12-03 01:46:57

This error may arises due to the incorrect template directories Try some change on settings.py as below

import os.path
Temp_Path = os.path.realpath('.')
...
STATIC_ROOT = ''    
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'
...
TEMPLATE_DIRS = (
    Temp_Path +"/template"
)

Then put all your template inside template folder and css/javascript file in static folder which is located inside your application folder. Hope this will solve your problem.

My suggestion don't put template folder inside application folder

Django will choose the first template it finds whose name matches, and if you had a template with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the easiest way to ensure this is by namespacing them. That is, by putting those templates inside another directory named for the application itself.

For Django 1.8 or above you have to add the template path in the DIRS key of TEMPLATES list as a list of string.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['Secondjango/Secondjango/templates/welcome'],        #<<<<<<<<Here
        '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',
            ],
        },
    },
]

Try this:

In your settings.py file replace

 
TEMPLATE_DIRS = (       
                  os.path.join(PROJECT_DIR, 'templates'),

)
 

with

 
TEMPLATE_DIRS = (       
                  os.path.join(PROJECT_DIR, 'templates'),
                  os.path.join(PROJECT_DIR, 'templates/welcome')

)
 

Then, in your code, just call render_to_response("home.html")

That should resolve your issue.

You can use: in settigns.py

TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), '..', 'templates').replace('\\','/'),)


STATICFILES_DIRS = (
    'static',
)
vsn harish rayasam

For Django 1.8 or above just Add the following in TEMPLATES DIR variable list in settings file

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': ['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',
        ],
    },
},
]

This property points to template directory 'DIRS': ['templates'],

For Django 2.0, I edited the value DIRS of TEMPLATES in setting file to the entry directory of the project:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': ['.'], # here set DIRS to project's entry directory
    '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',
        ],
    },
},

]

and the problem solved on both Mac and Windows systems.

suppose that you hace a django project "my_project", and the application "app_1" and "app_2"

my_project
    -- my_project  
    -- manage.py
    welcome
        -- __init__.py
        templates               
            -- home.html
        -- models.py
        -- views.py

    app_1
        -- __init__.py
        templates
            welcome
                -- home.html
        -- models.py
        -- views.py
    app_2
        templates
            a_subfolder
               -- home.html
        -- __init__.py
        -- models.py
        -- views.py

now, if you have some settings like.

#...
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',

)
#...

INSTALLED_APPS = (
    'app_2', 
    'app_1',
    'welcome',
    #...
)

#...

django will do that: When you call render_to_response('welcome/home.html') (I think that some params are missing), django will look at "home.html" file in some some "welcome" folder in ALL aplications (in this case "app_1" and "app_2") that have a "templates" folder. (that is a convention)

EDIT

I have added you "welcome" app.

You have to call the method without app name render_to_response('home.html')

Check if there is any include tag in "hello.html" which has been given filepath that does not exist. The error raised comes up the same :-

TemplateDoesNotExist at /hello/ /welcome/home.html

Please check if you have added your new application in the settings.py file under INSTALLED_APPS

Django compiles all the 'templates' folder from all the applications inside your project into a single 'templates' folder.

Please remember to create separate directory inside your applications 'template' folder to make it easier for the Django framework to find the template you are looking for.

Add template directory to your TEMPLATES in your project settings.py by 'DIRS': [os.path.join(BASE_DIR, 'templates')],

So the final TEMPLATES should be something like the following

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

Do this

-> Make sure your templates folder should be in root folder not in app folder -> then goto your setting.py file and look for TEMPLATES = [ 'DIRS': ] -> in DIRS put the path of your template folder like this 'DIRS':[r'path']

it will work

Another reason could be if the name of the folder in your app is template and not templates. Note 's' in the end.

Your settings.py file must be :

'DIRS': ["templates"]

Your templates folder must be under your main project name :

Like that

kanishk

Have you created the file __init__.py inside your welcome folder?

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