基于模板的django表单绘制

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


Build StatusCodecovVersionLicense

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.Formfroms.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]

运行测试

测试套件需要toxtox-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

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

推荐PyPI第三方库


热门话题
JavaI从另一个Jframe类B扩展了一个J帧类a,但在显示帧B时,它也在B中播放帧a   java适配器模式应用程序   使用并行流从java列表中获取Pojo,而不使用任何类型的索引   java更新文本文件中的双精度   java从webservice返回自定义对象   java在查找多个集合的交集时使用Retainal()的最快顺序   java如何在安卓社交媒体应用程序中应用用户对用户支付?   c代码能引发java异常吗?   Java数组填充了最后一个插槽中的内容   java JOOQ从大表中获取数据:良好实践   使用ApachePOI在JavaSwing中嵌入excel工作表   java如何使用枚举定义常量值组   java JavaFX将文本追加到TextArea引发异常   javascript JS在firefox中无法正常工作   java如何在同一片段中的片段打开后编辑该片段打开的活动中的值   对夏洛克的命令。py的解释不同于linux命令行和java进程api   php在Java中动态创建全局多维数组   html Java XML Transformer将“\n”替换为空格   Javac编译编码问题