简单的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第三方库


热门话题
爪哇太阳报。安全验证器。ValidatorException:PKIX路径生成失败   java理解为什么在onDispatchTouchEvent()返回True后仍调用onClick()   java如何在资源包中使用JSF标记/如何在资源包中重写URL?   java什么是流控制异常的替代方案?   java使用Spring数据JPA/MongoDB交叉存储,一个查询可以跨越两个数据库吗?   Apache Sling/felix中的java OSGi slf4j日志记录   为什么当我们想要水平/垂直旋转矩阵时,我们要把这个项除以2?   尝试从外部网页接收JSON字符串时出现安卓 Java NullPointerException   java执行异常:从Callable调用方法时   java在jetty上以调试模式运行webapp,使用maven jetty插件在intellij中构建成功(应用程序应在调试模式下启动)   带有内存数据库的linux Java应用程序的构建时间太长   基于java的随机数单元测试算法   java类型javax。摆动JComponent无法解析   Google日历API和UI小部件Java   java tomcat没有突然和任意地响应   java无法使用jsoup在html中获取图像src   我无法让Java接受键盘输入字符串   java如何开始使用Bambol而不让部署永远继续?   java如何使用另一个类的actionPerformed方法删除一个类/组件中的按钮?   java JSON反序列化brakets{}中的一系列对象