Skip to content
This repository has been archived by the owner on Feb 7, 2019. It is now read-only.

canonical-web-and-design/django-template-finder-view

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Archived

This project has been superseded by canonicalwebteam.django-views, which contained an equivalent TemplateFinder view class. Projects shouldb upgrade to using that package instead.


Django template finder view

TemplateFinder is an extension of TemplateView which attempts to load the corresponding templates directly from URLs, without the need to write a view for each URL.

Matching example

When Fenchurch parses a URL, it will look for templates in two corresponding locations, e.g.:

http://example.com/parent/location/ => TEMPLATE_DIR/parent/location.html

Or:

http://example.com/parent/location/ => TEMPLATE_DIR/parent/location/index.html

Installation

pip install django-template-finder-view

Usage

Simply include TemplateFinder in a URL match in your urls.py file, passing through the template argument:

# [app]/urls.py

from django_template_finder_view import TemplateFinder

...

urlpatterns = [
    ...
    url(r'^(?P<template>.*)$', TemplateFinder.as_view()),
]

Configuration

TemplateFinder will use the default Django template loader to search for templates with the expected names, so the simplest way to configure the templates location is to use the default TEMPLATE_DIRS setting, e.g.:

# [app]/settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
    }
]

A custom folder to load the templates can be specified. This allows you to keep the URL acessible templates separately from template includes.

# As a subfolder in templates. Only searching inside `templates/pages/`
TEMPLATE_FINDER_PATH = 'pages'

To specify an absolute directory outside templates, it must be added to the DIRS setting in TEMPLATES.

# Outside the templates folder
TEMPLATE_FINDER_PATH = os.path.join(BASE_DIR, 'pages')
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),
            TEMPLATE_FINDER_PATH,
        ],
    }
]

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages