Skip to main content

Simple Django utility that allows you to find templates with names matching given pattern

Project description

https://travis-ci.org/TyMaszWeb/django-template-finder.svg?branch=master

django-template-finder is a simple Django utility that allows you to find templates with names matching given pattern.

Suppose you are writing an app and want to allow your user to choose one of predefined templates, but don’t want to hardcode their names in any way. Scan for files? What if they are not store in the same directory? This is where django-template-finder can help you!

Supported template loaders:

  • django.template.loaders.app_directories.Loader

  • django.template.loaders.filesystem.Loader

  • django.template.loaders.cached.Loader

Contributions and comments are welcome using Github at: http://github.com/TyMaszWeb/django-template-finder

Installation

  1. pip install django-template-finder

That’s it! It’s just a simple utility library, no need to add anything to INSTALLED_APPS.

You will need a recent version of Django. Chances are django-template-finder will work with Django 1.2+, however only versions above 1.3 are supported.

Usage

Search for all 404.html templates, recursively, in all template loaders:

from templatefinder import find_all_templates

find_all_templates('404.html')

Search for all 4xx.html templates, recursively, in all template loaders:

from templatefinder import find_all_templates

find_all_templates('4*.html')

Search for all templates under menu/, recursively, in all template loaders:

from templatefinder import find_all_templates

find_all_templates('menu/*')

Generate nicer, human-friendly names for discovered templates in forms:

from templatefinder import find_all_templates, template_choices
from django.forms.widgets import Select

class MyForm(Form):
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        found_templates = find_all_templates('menu/*')
        choices = template_choices(templates=found_templates, display_names=None)
        self.fields['myfield'].widget = Select(choices=list(choices))

Providing human-friendly names for discovered templates, overriding the built-in name calculation:

from templatefinder import find_all_templates, template_choices

found_templates = find_all_templates('menu/*')
choices = template_choices(templates=found_templates, display_names={
    'menu/menu.html': 'My super awesome menu',
})

Using a project-wide setting for overriding the template display names:

from django.conf import settings
# note: this should be in your Django project's settings module, and is
# only set here for illustration purposes.
settings.TEMPLATEFINDER_DISPLAY_NAMES = {
    'menu/menu.html': 'Super menu',
    'menu/another-menu.html': 'Another menu',
}

from templatefinder import find_all_templates, template_choices

found_templates = find_all_templates('menu/*')
choices = template_choices(found_templates)

Bugs & Contribution

Please use Github to report bugs, feature requests and submit your code: http://github.com/TyMaszWeb/django-template-finder

author:

Piotr Kilczuk

date:

2013/03/27

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-template-finder-0.6.2.tar.gz (6.3 kB view details)

Uploaded Source

File details

Details for the file django-template-finder-0.6.2.tar.gz.

File metadata

File hashes

Hashes for django-template-finder-0.6.2.tar.gz
Algorithm Hash digest
SHA256 157bc0651e8965162e4ff8de6976d9613fc2df74f9baeab4945a0f5d60f2cf3e
MD5 98657ed009d686abcbaf145231b8ffdf
BLAKE2b-256 3113a8de9beff6d5afe8cb09c5903109db68a18244b84ff0f3fb9620ef25cb6c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page