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


热门话题
GridLayout不工作的java问题   java Restlet请求对象不包含身份验证信息   java使用Cucumber为每个正在运行的线程创建一个TestNG runner类   java在平面文件中存储并跟踪它们?   类Lcom/google/firebase/FirebaseApp中没有虚拟方法zzbzo()Z;或者它的超类   java如何在安卓中创建字符串模式   可从Java访问的树莓PI传感器   java 50+GB分隔文件中的大致行数   是否有Java进程内数据库,允许从多表虚拟机访问?   当我使用ArrayList时,HashMap中的Java ArrayList会导致HashMap中的数据被删除。去除   如何将值存储在最佳java集合结构中   java消息Servlet。servlet appServlet的init()引发异常   java正在使用Spring引导生成空日志文件   c#在访问实例变量时使用this关键字是否更有效?   java JScrollPane滚动到最后添加的行   java Vertx抛出IllegalStateException:响应已被写入   java Liferay以编程方式更改portlet列   java下载单个s3对象并将它们合并到单个文件中   java spark数据帧将JSON转换为ORC满足“列模糊异常”