基于模板的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第三方库


热门话题
我们可以使用java for loop with switch语句。。?   java如何从具有循环关系的数据集生成树?   使用s:mvcUrl标记时java Spring启动问题   javascript Wicket调色板水平滚动条在firefox中不显示   java如何清理Maven插件绑定?   java将文件从设备发送到计算机不起作用(文件被破坏)   Java GPU编程的性能   java字符串标记器问题   连接Java LDAP断开连接   java Android GridView将无法确定正确的位置   java AlarmManager在指定时间未显示toast消息   MVC中的java测试外观|断言返回类型   logcat上的java Android运行时错误   java Building Workspace在Eclipse中遇到错误   堆栈溢出尾部递归遗传算法抛出“awteventque0”java。StackOverflowerr语言   java ArrayOutOfBounds测试代码不工作   java有没有像Moodle这样流行的课程管理系统?   java如何从jsp获取xml文件作为响应