基于模板的django表单绘制
django-template-forms的Python项目详细描述
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_formsform类都继承自 django.forms.BaseForm,并打算与混凝土混合 forms.Form或froms.ModelForm。示例用法:
fromdjangoimportformsfromtemplate_formsimportbs3from.modelsimportArticleclassArticleForm(bs3.BlockForm,forms.ModelForm):classMeta:model=Articlefields=['title','author','published_date',...]
在view模板中:
<formmethod="post"action=""> {% csrf_token %} {{ form }} <buttonclass="btn btn-primary"type="submit">Save</button></form>
todo:自定义TemplateForm子类的文档创建。
覆盖模板
django-template-forms提供一组默认模板。到 忽略这些,首先需要考虑 `FORM_RENDERER<;https://docs.djangoproject.com/en/1.11/ref/settings/#std:setting-FORM_RENDERER>;。`_ 设置,然后覆盖相关模板文件默认情况下, FORM_RENDERER使用 `DjangoTemplates<;https://docs.djangoproject.com/en/1.11/ref/forms/renderers/#djangotemplates>;。`_ 渲染器,从已安装的应用程序加载模板。
如果希望对模板加载进行更多控制,可以使用 `TemplatesSetting<;https://docs.djangoproject.com/en/1.11/ref/forms/renderers/#templatessetting>;。`_ 渲染器,它使用配置的^{tt14}加载模板$ 设置。如果使用此渲染器,可能需要添加 'django.forms'到您的INSTALLED_APPS。见renderer docs 更多信息。
INSTALLED_APPS=[...'django.forms','template_forms',]FORM_RENDERER='django.forms.renderers.TemplatesSetting'TEMPLATES=[{'BACKEND':'django.template.backends.django.DjangoTemplates','DIRS':[...],'APP_DIRS':True,...},]
为每个支持的css框架提供模板。
^ {STR 1 } $注:< /强> -BS 4还不存在水平布局。-检查&; 无线电领域模板还不存在。
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
- [删除线:template_forms/bs3/fields/block_check_inline.html]
- [删除线:template_forms/bs3/fields/block_check_stacked.html]
- [删除线:template_forms/bs3/fields/block_radio_inline.html]
- [删除线:template_forms/bs3/fields/block_radio_stacked.html]
- template_forms/bs3/fields/horizontal.html
- template_forms/bs3/fields/horizontal_checkbox.html
- [删除线:template_forms/bs3/fields/horizontal_check_inline.html]
- [删除线:template_forms/bs3/fields/horizontal_check_stacked.html]
- [删除线:template_forms/bs3/fields/horizontal_radio_inline.html]
- [删除线:template_forms/bs3/fields/horizontal_radio_stacked.html]
bs4:
- template_forms/bs4/forms/block.html
- [删除线:template_forms/bs4/forms/horizontal.html]
- template_forms/bs4/fields/block.html
- [删除线:template_forms/bs4/fields/block_checkbox.html]
- [删除线:template_forms/bs4/fields/block_check_inline.html]
- [删除线:template_forms/bs4/fields/block_check_stacked.html]
- [删除线:template_forms/bs4/fields/horizontal.html]
- [删除线:template_forms/bs4/fields/horizontal_checkbox.html]
- [删除线:template_forms/bs4/fields/horizontal_check_inline.html]
- [删除线: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有关详细信息,请参见许可证。