简单的django实用程序,允许您查找名称与给定模式匹配的模板
django-template-finder的Python项目详细描述
django模板查找器是一个简单的django实用程序,它允许您查找 名称与给定模式匹配的模板。
假设您正在编写应用程序,并希望允许用户选择 预定义模板,但不希望以任何方式硬编码其名称。扫描 为了文件?如果它们不是存储在同一个目录中呢?这里就是 django模板查找器可以帮助您
支持的模板加载程序:
- django.template.loaders.app_directories.Loader
- django.template.loaders.filesystem.Loader
- django.template.loaders.cached.Loader
欢迎使用github发表意见: http://github.com/TyMaszWeb/django-template-finder
安装
- pip安装django模板查找程序
就这样!它只是一个简单的实用程序库,不需要添加任何内容 INSTALLED_APPS。
你需要一个最新版本的django。可能是django模板查找器 将与Django 1.2+一起使用,但是仅支持1.3以上的版本
用法
在所有模板加载程序中递归搜索所有404.html模板:
from templatefinder import find_all_templates find_all_templates('404.html')
在所有模板加载程序中递归搜索所有4xx.html模板:
from templatefinder import find_all_templates find_all_templates('4*.html')
在所有模板加载程序中递归搜索menu/下的所有模板:
from templatefinder import find_all_templates find_all_templates('menu/*')
为表单中发现的模板生成更好的、人性化的名称:
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))
为发现的模板提供友好的名称,覆盖内置的 名称计算:
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', })
使用项目范围的设置覆盖模板显示名称:
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)
错误和贡献
请使用github报告错误、功能请求并提交代码: http://github.com/TyMaszWeb/django-template-finder
author: | Piotr Kilczuk |
---|---|
date: | 2013/03/27 |