如何将 Django forms.ChoiceField 渲染为 Twitter Bootstrap 下拉菜单 [英] How to render Django forms.ChoiceField as Twitter Bootstrap dropdown

查看:19
本文介绍了如何将 Django forms.ChoiceField 渲染为 Twitter Bootstrap 下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 django-bootstrap、django-bootstrap-form、django-bootstrap 之一呈现 Django 表单的最有效方式是什么(在编程/维护工作、优雅方面).ChoiceField 作为 Twitter Bootstrap 下拉列表工具包、django-crispy-forms 等应用程序?在这些应用中是否有明确支持此用例?

What is the most efficient way (in terms of programming/maintenance effort, elegance) to render a Django forms.ChoiceField as a Twitter Bootstrap dropdown using one of the django-bootstrap, django-bootstrap-form, django-bootstrap-toolkit, django-crispy-forms, etc apps? Is there explicit support for this use case in any of these apps?

推荐答案

免责声明 我是 django-crispy-forms(提到的应用程序之一)的首席开发人员.

Disclaimer I'm the lead developer of django-crispy-forms (one of the apps mentioned).

我将尝试解释您如何使用 django-crispy-forms 执行此操作.您只需在模板中执行:

I will try to explain how you do this with django-crispy-forms. You simply do in your template:

{% load crispy_forms_tags %}
{{ form|crispy }}

您可以在 django-crispy-forms 文档中看到这一点以及更多内容.您的 ChoiceField 将根据需要呈现为 Bootstrap 下拉列表.

You can see this and more in django-crispy-forms docs. Your ChoiceField will be rendered as Bootstrap dropdown as you want.

首先,一点点历史.django-bootstrap 是在 django-uni-form(django-crispy-forms 演变而来的父项目)之后诞生的.当时,django-uni-form 已经在做 Boostrap 表单,但可能不是最好的方式(Bootstrap 是通过使用附加的 contrib 应用程序来支持的).因此,django-bootstrap 的作者可能决定自己动手.

First, a little bit of history. django-bootstrap was born after django-uni-form (the parent project from which django-crispy-forms evolved). At that time, django-uni-form was already doing Boostrap forms, but probably not in the best possible way (Bootstrap was supported by using an aditional contrib application). Thus, the author of django-bootstrap probably decided to go on its own.

现在,关于 Bootstrap 支持.django-bootstrap 也可以渲染表单,但它不使用 Django 过滤器,而是更改表单的基类.所以 django-crispy-forms 会影响你的模板,而 django-bootstrap 会影响你的 Python 代码.

Now, regarding Bootstrap support. django-bootstrap can also render forms but, instead of using a Django filter, it changes the base class of your form. So django-crispy-forms affects your templates while django-bootstrap affects your Python code.

此外,django-crispy-forms 和 django-bootstrap 都可以让您进行布局.在 django-bootstrap 中,布局位于表单内的 Meta 类中,而在 django-crispy-forms 中,布局位于 FormHelper 的子类中,这使您可以解耦.

Also, both django-crispy-forms and django-bootstrap let you do layouts. In django-bootstrap, layouts are in a Meta class within the form while in django-crispy-forms the layouts live in a subclass of FormHelper, which gives you decoupling.

django-bootstrap 使用元组来定义布局,而crispy-forms 使用Layout 的子类.这增加了重用布局、轻松组合布局等的可能性.请注意,尽管crispy 的封装内部仍然有一个字段列表,但它添加了一个有用且人性化的 API 来以编程方式操作布局,并且我认为强制执行良好的解耦模式.

django-bootstrap uses a tuple for defining a layout, while crispy-forms uses a subclass of Layout. This adds the possibility to reuse layouts, compose layouts easily, etc. Note that although crispy's encapsulation still has a list of fields inside, it adds a helpful and human-friendly API to programmatically manipulate the layout and I think enforces a good decoupling pattern.

据我所知,脆形式的布局更强大.它有一个更大的布局对象集合,例如,前置文本、附加文本、日期范围等已经被支持,而在 django-boostrap 中这些都在 TODO 列表中.

From what I can see, layouts in crispy-forms are more powerful. It has a larger layout object collection, for example, prepended text, appended text, daterange and others are already supported while in django-boostrap these are in the TODO list.

crispy-forms 也有一个 API,用于在旅途中修改布局并进行一些非常好的核心编程布局构建.

crispy-forms has also an API for modifying layouts on the go and doing some hardcore programmatic layout building which is very nice.

crispy-forms 还支持所有类型的表单集.它支持不同的 CSS 模板包,这意味着如果将来新的 kicking CSS 包被命名为chocolate",那么为它创建一个新的模板包将非常容易,并且您的所有表单都可以使用"Chocolate' 无需更改代码,只是一个简单的设置变量.

crispy-forms also supports formsets of all kinds. It supports different CSS template packs, which means that if in the future the new kicking CSS pack is named 'chocolate', it will be very easy to create a new template pack for it and all your forms will be able to be rendered with 'chocolate' without code changes, just a simple setting variable.

crispy-forms 也有 属性 你可以在 FormHelper 中设置,它定义了很好的额外功能,你可以很容易地打开和关闭.如果需要,您还可以创建自己的自定义属性.

crispy-forms also has attributes you can set in FormHelper that define nice extra functionaly you can easily turn on and off. You can also create your own custom attributes if you want.

最后,django-crispy-forms(连同 django-uni-form)有超过 67.000 次下载,这对于 Django 应用程序来说是相当不错的.该项目在 Github 有近 500 名关注者,几个大用户,良好的测试覆盖率和几年的历史,并且仍在积极维护中.

Finally, django-crispy-forms (together with django-uni-form) has more than 67.000 downloads, which is quite good for a Django application. The project has almost 500 followers in Github, several big users, good testing coverage and several years of history and it's still actively maintained.

据我所知,django-bootstrap-form 只是一个用于使用 Bootstrap 渲染表单的过滤器.这是 django-crispy-form 涵盖的东西,同时提供了很多很多.该项目于 2012 年 8 月 21 日发布,在我看来就像是在重新发明轮子,因为其他几个应用程序已经涵盖了这个用例.

From what I can see django-bootstrap-form is only a filter for rendering a form with Bootstrap. That is something django-crispy-form covers while offering much, much more. The project was released on 21st August 2012 and looks to me like it's reinventing the wheel because several other apps cover already this use case.

它的灵感来自 django-boostrap-form.从我在文档中看到的内容来看,它还为您提供了一个用于使用 Bootstrap 呈现表单的过滤器.它显然涵盖了比表单更多的 Bootstrap 内容,但我在其文档中找不到更多信息.上次提交是在 2 个月前.

It's inspired by django-boostrap-form. From what I see in the docs, it also gives you a filter for rendering a form with Bootstrap. It apparently covers more Bootstrap stuff than forms, but I can't find more info in its docs. Last commit was 2 months ago.

我会坚持认为,我显然不是进行没有偏见的比较的合适人选.这就是为什么我以前从来没有写过这个.我本可以多次发布关于此的博客文章,但我总是拒绝这个想法.然而,随着表单应用程序(和引导支持应用程序)的碎片化程度越来越高,我认为这可能是写下我的想法的好时机.我希望这对 Django 的新手有所帮助.

I will insist that I'm obviously not the right person for a comparison that is not biased. That's why I've never written about this before. I could have published a blog post about this several times but I always dismissed the idea. However, as the fragmentation of form apps (and bootstrap-support apps) is growing, I thought this might be a good time to write down what I think. I hope this helps newcomers to Django.

干杯,米格尔

这篇关于如何将 Django forms.ChoiceField 渲染为 Twitter Bootstrap 下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆