django酥脆形式的内联形式 [英] django crispy-forms inline forms

查看:80
本文介绍了django酥脆形式的内联形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试采用脆弱形式和引导,尽可能多地使用它们的功能,而不是重复发明一些东西。



是否有使用像django-admin表单这样的脆性表单/引导程序的内联表单功能的方法有?



这里是一个例子:



pre> class NewProjectForm(forms.Form):
name = forms.CharField(required = True,label = _(u'Названиепроекта'),widget = forms。 TextInput(attrs = {'class':'input-block-level'}))
group = forms.ModelChoiceField(required = False,queryset = Group.objects.all(),label = _(u'Группа проектов'),widget = forms.Select(attrs = {'class':'input-block-level'}))
description = forms.CharField(required = False,label = _(u'Описаниепроекта' ),widget = forms.Textarea(attrs = {'class':'input-block-level'}))

class Meta:
model = Project
fields = NA
$ b def __init __(self,* args,** kwargs):
self.helper = FormHelper()
self.helper .form_class ='horizo​​ntal-form'
self.helper.form_action ='submit_new_project'
self.helper.layout = Layout(
Field('name',css_class ='input-block- level'),
Field('group',css_class ='input-block-level'),
Field('description',css_class ='input-block-level'),

self.helper.add_input(Submit('submit',_(u'Создатьпроект')))
self.helper.add_input(Submit('cancel',_(u'Япередумал' )))
super(NewProjectForm,self).__ init __(* args,** kwargs)

它将显示一个体面的形式:





我如何去ab添加一个基本代表这个模型的表单:

  class Link(models.Model):
name = models。 CharField(max_length = 255,blank = False,null = False,verbose_name = _(u'Название'))
url = models.URLField(blank = False,null = False,verbose_name = _(u'Ссылка' ))
project = models.ForeignKey('Project')

所以会有一个项目和名称/ url链接和方法来添加许多,像在django-admin中一样的事情完成,您可以在其中添加额外的行与您的主模型相关的数据。在下面的sreenshot中,您可以填写Question对象的数据,下面可以添加QuestionOption对象的数据,您可以点击+图标,根据需要添加任意数量的QuestionOptions。 p>

我不是想找到一种方法来获取模型自动生成的表单(这很好,但不是最重要的) - 是否有一种方法来构建一个表单,你可以添加django-admin这样的数据行吗?



解决方案

您引用的内联表单在Django中被称为Formsets。如果您想了解更多关于formets的信息,可以在 Django表单文档中找到它们



Crispy支持渲染内联表单,如渲染表单部分



请注意,默认情况下,formets会显示3个额外的内联表单,允许您再添加三个对象。保存后,您再次获得三个额外的内联表单以添加更多。



如果您想要像Django Admin中的添加更多按钮,则需要使用一些Javascript动态添加这些行。


I'm trying to adopt crispy-forms and bootstrap and use as much of their functionality as possible instead of inventing something over and over again.

Is there a way to have inline forms functionality with crispy-forms/bootstrap like django-admin forms have?

Here is an example:

class NewProjectForm(forms.Form):
    name = forms.CharField(required=True, label=_(u'Название проекта'), widget=forms.TextInput(attrs={'class':'input-block-level'}))
    group = forms.ModelChoiceField(required=False, queryset=Group.objects.all(), label=_(u'Группа проектов'), widget=forms.Select(attrs={'class':'input-block-level'}))
    description = forms.CharField(required=False, label=_(u'Описание проекта'), widget=forms.Textarea(attrs={'class':'input-block-level'}))

    class Meta:
        model = Project
        fields = ('name','description','group')

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_class = 'horizontal-form'
        self.helper.form_action = 'submit_new_project'
        self.helper.layout = Layout(
            Field('name', css_class='input-block-level'),
            Field('group', css_class='input-block-level'),
            Field('description',css_class='input-block-level'),
        )
        self.helper.add_input(Submit('submit',_(u'Создать проект')))
        self.helper.add_input(Submit('cancel',_(u'Я передумал')))
        super(NewProjectForm, self).__init__(*args, **kwargs)

it will display a decent form:

How do I go about adding a form that basically represents this model:

class Link(models.Model):
    name = models.CharField(max_length=255, blank=False, null=False, verbose_name=_(u'Название'))
    url = models.URLField(blank=False, null=False, verbose_name=_(u'Ссылка'))
    project = models.ForeignKey('Project')

So there will be a project and name/url links and way to add many, like same thing is done in django-admin where you are able to add extra 'rows' with data related to your main model. On the sreenshot below you are able to fill out data for 'Question' object and below that you are able to add data for QuestionOption objects -you are able to click the '+' icon to add as many QuestionOptions as you want.

I'm not looking for a way to get the forms auto-generated from models (that's nice but not the most important) - is there a way to construct a form that will let you add 'rows' of data like django-admin does?

解决方案

The inline forms you refer to, are known in Django as Formsets. If you want to know more about formsets, you can find them in the Django forms documentation.

Crispy supports rendering inline formsets, as described in the section 'Rendering Formsets'

Please note that formsets by default show 3 extra inline forms, allowing you to add three more objects. After saving, you again get three extra inline formsets to add even more.

If you want an 'add more' button like in the Django Admin, you will need to use some Javascript to dynamically add those rows.

这篇关于django酥脆形式的内联形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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