简单的django实用程序,允许您查找名称与给定模式匹配的模板

django-template-finder的Python项目详细描述


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

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

安装

  1. 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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
jenkins在java代码的csv中使用前导零传递数字   终止java应用程序javaw。exe eclipse   java平均字长。文本   java在事务中关闭连接时会发生什么?   java如何为rich faces extendedDatatable启用可调整大小的列?   SpringJavaWebApp头nosniff不适合js和css文件   Java:抛出异常   java获取拒绝ArrayList中存在的用户名的代码。   关于编写JNDI服务提供者的java教程   java Android emulator电子书示例已停止   java如何防止双向关系中的循环   在Java中,如何将接口的实现作为变量传递给方法?   java有没有办法通过注释来监听CoreNLP处理的进度?   java Eclipse错误:导入项目时“无法读取项目描述文件”   java为什么findViewById返回null?