Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the path that Django uses for locating and loading templates?

I'm following this tutorial on a Windows 7 environment.

My settings file has this definition:

TEMPLATE_DIRS = (     'C:/django-project/myapp/mytemplates/admin' ) 

I got the base_template from the template admin/base_site.html from within the default Django admin template directory in the source code of Django itself (django/contrib/admin/templates) into an admin subdirectory of myapp directory as the tutorial instructed, but it doesn't seem to take affect for some reason.

Any clue of what might be the problem?

like image 338
shaytac Avatar asked Jun 14 '10 15:06

shaytac


People also ask

What does Django use for templates?

A Django template is a text document or a Python string marked-up using the Django template language. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags. A template is rendered with a context.

Which keyword is used to load another template in template file in Django?

include tag loads a template and renders it with the current context. This is a way of “including” other templates within a template.

What does {% %} do in Django?

{% extends variable %} uses the value of variable . If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.

What is {% load static %} in Django?

The {% static %} template tag generates the absolute URL of static files. That's all you need to do for development. Reload http://localhost:8000/polls/ and you should see that the question links are green (Django style!) which means that your stylesheet was properly loaded.


2 Answers

I know this isn't in the Django tutorial, and shame on them, but it's better to set up relative paths for your path variables. You can set it up like so:

import os.path  PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))  ...  MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media/')  TEMPLATE_DIRS = [     os.path.join(PROJECT_PATH, 'templates/'), ] 

This way you can move your Django project and your path roots will update automatically. This is useful when you're setting up your production server.

Second, there's something suspect to your TEMPLATE_DIRS path. It should point to the root of your template directory. Also, it should also end in a trailing /.

I'm just going to guess here that the .../admin/ directory is not your template root. If you still want to write absolute paths you should take out the reference to the admin template directory.

TEMPLATE_DIRS = [     'C:/django-project/myapp/mytemplates/', ] 

With that being said, the template loaders by default should be set up to recursively traverse into your app directories to locate template files.

TEMPLATE_LOADERS = [     'django.template.loaders.filesystem.load_template_source',     'django.template.loaders.app_directories.load_template_source',     # 'django.template.loaders.eggs.load_template_source', ] 

You shouldn't need to copy over the admin templates unless if you specifically want to overwrite something.

You will have to run a syncdb if you haven't run it yet. You'll also need to statically server your media files if you're hosting django through runserver.

like image 93
digitaldreamer Avatar answered Sep 24 '22 04:09

digitaldreamer


If using Django settings as installed, then why not just use its baked-in, predefined BASE_DIR and TEMPLATES? In the pip installed Django(v1.8), I get:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))    TEMPLATES = [     {         'BACKEND': 'django.template.backends.django.DjangoTemplates',         'DIRS': [             ### ADD YOUR DIRECTORY HERE LIKE SO:             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',             ],         },     }, ] 
like image 23
NathanQ Avatar answered Sep 20 '22 04:09

NathanQ