基于模板的Django表单渲染
项目描述
基于模板的Django表单渲染
概述
Django 1.11最近引入了基于模板的小部件渲染功能,以便用户更容易地自定义小部件。 django-template-forms 是该功能的逻辑扩展,实现了字段和表单的基于模板的渲染。
目前,支持Bootstrap 3 & 4的简单块表单。水平表单和复选框/单选字段布局即将到来。
需求
Python: 3.4, 3.5, 3.6
Django: 1.11, 2.0b
使用pip安装
$ pip install django-template-forms
配置
安装后,将'template_forms'添加到您的INSTALLED_APPS中。
INSTALLED_APPS = [
...
'template_forms',
]
使用
所有 template_forms 表单类都继承自 django.forms.BaseForm,并旨在与具体的 forms.Form 或 froms.ModelForm 混合使用。示例使用
from django import forms
from template_forms import bs3
from .models import Article
class ArticleForm(bs3.BlockForm, forms.ModelForm):
class Meta:
model = Article
fields = ['title', 'author', 'published_date', ...]
在 视图 模板中
<form method="post" action="">
{% csrf_token %}
{{ form }}
<button class="btn btn-primary" type="submit">Save</button>
</form>
待办事项: 记录自定义 TemplateForm 子类的创建。
覆盖模板
django-template-forms 提供了一组默认模板。要覆盖这些模板,首先需要考虑设置 `FORM_RENDERER <https://docs.django.ac.cn/en/1.11/ref/settings/#std:setting-FORM_RENDERER>`__,然后覆盖相关的模板文件。默认情况下,FORM_RENDERER 使用 `DjangoTemplates <https://docs.django.ac.cn/en/1.11/ref/forms/renderers/#djangotemplates>`__ 渲染器,它从您安装的应用程序中加载模板。
如果您想要对模板加载有更多的控制,可以使用 `TemplatesSetting <https://docs.django.ac.cn/en/1.11/ref/forms/renderers/#templatessetting>`__ 渲染器,它使用您配置的 TEMPLATES 设置来加载模板。如果您使用此渲染器,可能需要将 'django.forms' 添加到您的 INSTALLED_APPS。有关更多信息,请参阅 渲染器文档。
INSTALLED_APPS = [
...
'django.forms',
'template_forms',
]
FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [...],
'APP_DIRS': True,
...
},
]
为每个支持的 CSS 框架提供了模板。
注意: - 目前还没有为 BS 4 提供 horizontal 布局。 - 检查 & 单选字段模板目前不存在。
BS3
template_forms/bs3/forms/block.html
template_forms/bs3/forms/horizontal.html
template_forms/bs3/fields/block.html
template_forms/bs3/fields/block_checkbox.html
[STRIKEOUT:template_forms/bs3/fields/block_check_inline.html]
[STRIKEOUT:template_forms/bs3/fields/block_check_stacked.html]
[STRIKEOUT:template_forms/bs3/fields/block_radio_inline.html]
[STRIKEOUT:template_forms/bs3/fields/block_radio_stacked.html]
template_forms/bs3/fields/horizontal.html
template_forms/bs3/fields/horizontal_checkbox.html
[STRIKEOUT:template_forms/bs3/fields/horizontal_check_inline.html]
[STRIKEOUT:template_forms/bs3/fields/horizontal_check_stacked.html]
[STRIKEOUT:template_forms/bs3/fields/horizontal_radio_inline.html]
[STRIKEOUT:template_forms/bs3/fields/horizontal_radio_stacked.html]
BS4
template_forms/bs4/forms/block.html
[STRIKEOUT:template_forms/bs4/forms/horizontal.html]
template_forms/bs4/fields/block.html
[STRIKEOUT:template_forms/bs4/fields/block_checkbox.html]
[STRIKEOUT:template_forms/bs4/fields/block_check_inline.html]
[STRIKEOUT:template_forms/bs4/fields/block_check_stacked.html]
[STRIKEOUT:template_forms/bs4/fields/horizontal.html]
[STRIKEOUT:template_forms/bs4/fields/horizontal_checkbox.html]
[STRIKEOUT:template_forms/bs4/fields/horizontal_check_inline.html]
[STRIKEOUT:template_forms/bs4/fields/horizontal_check_stacked.html]
运行测试
测试套件需要 tox 和 tox-venv。可以使用 tox -l 查看完整的构建列表。
$ pip install tox tox-venv
$ tox -e py36-django111
$ tox -e lint,isort
发布流程
在 setup.py 中更新软件包版本
为版本创建 git 标签
上传发布到 PyPI sh $ pip install -U setuptools wheel pypandoc $ rm -rf dist/ build/ $ python setup.py bdist_wheel upload
版权 & 许可证
版权 © 2017 Ryan P Kilby。有关详细信息,请参阅 LICENSE。
项目详情
哈希值 for django_template_forms-0.3.1-py3-none-any.whl
算法 | 哈希摘要 | |
---|---|---|
SHA256 | f21f9ecebabf2bc65f4af38b79e92d09bcaca2402a80421c2f1c6712e46ba4ab |
|
MD5 | 99c65d42d71580d2ffc5336e5e7f6253 |
|
BLAKE2b-256 | f3c7e6be080eb289097a9004321889c98092b6fbca980e5ae4dc4ff837a5ba5b |