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

问题描述

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

解决方案

免责声明 django-crispy-forms(其中一个应用程序)的主要开发人员。



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

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

您可以在。您的ChoiceField将根据需要呈现为Bootstrap下拉列表。



与django-bootstrap

相比,

首先,有点历史。 django-bootstrap出生于django-uni-form(父类项目,django-crispy形式演化)。当时,django-uni-form已经在做Boostrap表单,但是可能不是以最好的方式(Bootstrap是通过使用一个aditional contrib应用程序来支持的)。因此,django-bootstrap的作者可能决定自行进行。



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



此外,django-crispy-forms和django-bootstrap都可以让您进行布局。在django-bootstrap中,布局位于窗体内的Meta类中,而在django-crispy中,布局生成在 FormHelper 的子类中,这样可以解耦。 p>

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



从我可以看到,脆饼形式的布局更强大。它具有较大的布局对象集合,例如,前缀文本,附加文本,daterange等等已被支持,而在django-boostrap中,这些都在TODO列表中。



脆性表单还有一个用于修改随身体布局的API,并进行一些非常好的核心程序化布局构建。



crispy-forms还支持各种形式的表单。它支持不同的CSS模板包,这意味着如果将来新的踢CSS包被命名为巧克力,那么为它创建一个新的模板包将是非常容易的,所有的表单将能够以巧克力没有代码更改,只是一个简单的设置变量。



crispy-forms也有,您可以在 FormHelper 中设置,定义好额外的功能你可以轻松打开和关闭。您还可以根据需要创建自己的自定义属性。



最后,django-crispy-forms(与django-uni-form一起)拥有超过67.000次下载,对Django应用程序来说相当不错。该项目在Github有近500名追随者,几个大用户,良好的测试覆盖率和几年的历史,并且仍然保持活跃。



与django-bootstrap-form



从我可以看到django-bootstrap-form只是一个使用Bootstrap呈现表单的过滤器。这是django-crispy形式涵盖的东西,而提供更多,更多。该项目于2012年8月21日发布,并且看起来像是重塑轮子,因为其他几个应用程序已经覆盖了这个用例。



与django-bootstrap-toolkit



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






我会坚持认为我显然不是正确的比较对象没有偏见这就是为什么我从来没有写过这个。我可以发表这个博客文章这几次,但我总是不理解这个想法。然而,随着表单应用程序(和引导支持应用程序)的分散化越来越多,我认为这可能是写下我的想法的好时机。我希望这能帮助新来者Django。



干杯,
米格尔


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?

解决方案

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

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 }}

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

Compared to 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.

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.

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

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 has also an API for modifying layouts on the go and doing some hardcore programmatic layout building which is very nice.

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

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.

Compared to django-bootstrap-form

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.

Compared to django-bootstrap-toolkit

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.


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.

Cheers,Miguel

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

10-29 02:04