1

It worked on localhost, but in production it is showing this error:

TemplateDoesNotExist at /tcapp/tcpage

app/urls.py

from django.conf.urls import url 
from .import views

urlpatterns = [
    url(r'^tcapp/tcpage', views.tcpage, name='tcpage'),
    url(r'^tcapp/tcappretrive', views.tcappretrive, name='tcappretrive'),
    url(r'^tcapp/home',views.tcpage,name='tcpage'),
    url(r'^tcapp/work',views.work,name='work'),
    url(r'^tcapp/s&c',views.support,name='support'),
]

templates:

  • home.html
  • work.html
  • s&c.html

What could be causing this error message?

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
Pranay reddy
  • 160
  • 1
  • 13

2 Answers2

1

Here might be the reasons for this-

  1. Does your settings includes the correct template directory ? You might need to check TEMPLATE_DIR settings in settings.py -

      TEMPLATES = [
                    {
                      'BACKEND': 'django.template.backends.django.DjangoTemplates',
                      'DIRS': [
                          '/home/html/example.com',
                          '/home/html/default',
                         ],
                     },
                   {
                    'BACKEND': 'django.template.backends.jinja2.Jinja2',
                    'DIRS': [
                            '/home/html/jinja2',
                            ],
                    },
        ]
    

    for more check out this

  2. Also, you might need to add a template directory using os.path function .

Let me know, if this solves your problem.

tom
  • 3,720
  • 5
  • 26
  • 48
0

Templates are set in the settings.py

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [
        '/var/www/rsa/html', #1) this sets a static path
        os.path.join( PROJECT_PATH, 'templates').replace('\\','/'), #2) this sets a dynamic path
    ],
    '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',
        ],
    },
},
]

the first 1) path

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))

just says all templates are available at this absolute path. not really recommended. I prefer the second option that sets templates as per app.

os.path.join( PROJECT_PATH, 'templates').replace('\\','/'),

just means that whatever apps you make using

./manage startapp app_name

inside that app folder, you can create a folder called "templates" and put whatever templates you want in there.

extend templates in the static folders, & have app-specific templates in the dynamic folder.

in an app called countries you can have a template in

/countries/templates/display.html

inside you can have

{% extends 'template/app.html' %} << this file is located in the static folder

{% block 'content' %}
  content goes here
{% endblock %}

Hope that helps.

p.s. templates are searched for in order they are listed so first the static then the dynamic location is looked at. if app.html is found in templates then django will stop there and wont look any further.

A H Bensiali
  • 825
  • 1
  • 9
  • 22