Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django templates folders

I'm experimenting with Django, and figuring out how to set urls.py, and how the URLs work. I've configured urls.py in the root of the project, to directs to my blog and admin. But now I want to add a page to my home, so at localhost:8000.

So I've added to following code to the urls.py in the root of the project:

from django.views.generic.simple import direct_to_template  urlpatterns = patterns('',     (r"^$", direct_to_template, {"template": "base.html"}), ) 

The problem is that it searches for the template in blog/templates/... Instead of the templates folder in my root. Which contains the base.html.

Full urls.py:

from django.conf.urls import patterns, include, url from django.views.generic.simple import direct_to_template  # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover()   urlpatterns = patterns('',     (r"^$", direct_to_template, {"template": "base.html"}),     url(r'^blog/', include('hellodjango.blog.urls')),     url(r'^admin/doc/', include('django.contrib.admindocs.urls')),     url(r'^admin/', include(admin.site.urls)),     (r'^tinymce/', include('tinymce.urls')), ) 

Am I overlooking something?

like image 221
Kevinvhengst Avatar asked Mar 14 '13 13:03

Kevinvhengst


People also ask

Where should template folder be in Django?

To configure the Django template system, go to the settings.py file and update the DIRS to the path of the templates folder. Generally, the templates folder is created and kept in the sample directory where manage.py lives. This templates folder contains all the templates you will create in different Django Apps.

What does the Django templates contain?

Being a web framework, Django needs a convenient way to generate HTML dynamically. The most common approach relies on templates. A template contains the static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted.

How do I organize my Django apps?

The way I like to organize my Django Project is – Keeps all Django apps in apps folder, static files (scripts, js, CSS) in the static folder, HTML files in templates folder and images and media content in the media folder.

What is static folder in Django?

django. contrib. staticfiles provides a convenience management command for gathering static files in a single directory so you can serve them easily. This will copy all files from your static folders into the STATIC_ROOT directory.


2 Answers

Did you set TEMPLATE_DIRS in your settings.py? Check and make sure it is set up correctly with absolute paths. This is how I make sure it is properly set:

settings.py

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))  TEMPLATE_DIRS = (     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".     # Always use forward slashes, even on Windows.     # Don't forget to use absolute paths, not relative paths.     os.path.join(PROJECT_ROOT, 'templates').replace('\\','/'), )  # List of callables that know how to import templates from various sources. TEMPLATE_LOADERS = (     'django.template.loaders.filesystem.Loader',     'django.template.loaders.app_directories.Loader', #     'django.template.loaders.eggs.Loader', ) 

This way, I have a templates folder in my project root that is used for non-app templates and each app has a templates/appname folder inside the app itself.

If you want to use a template from the root template folder, you just give the name of the template like 'base.html' and if you want to use an app template, you use 'appname/base.html'

Folder structure:

project/   appname/     templates/        appname/  <-- another folder with app name so 'appname/base.html' is from here         base.html     views.py     ...    templates/    <-- root template folder so 'base.html' is from here     base.html    settings.py   views.py   ... 
like image 180
Ngenator Avatar answered Oct 05 '22 16:10

Ngenator


from django.conf.urls import patterns, include, url  # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover()   urlpatterns = patterns('',     url(r'^blog/', include('hellodjango.blog.urls')),     url(r'^admin/doc/', include('django.contrib.admindocs.urls')),     url(r'^admin/', include(admin.site.urls)),     url(r'^tinymce/', include('tinymce.urls')), )  urlpatterns += patterns(     'django.views.generic.simple',     (r'^', 'direct_to_template', {"template": "base.html"}), ) 
like image 32
catherine Avatar answered Oct 05 '22 16:10

catherine