django crispy-forms inline forms

Posted by abolotnov on Stack Overflow See other posts from Stack Overflow or by abolotnov
Published on 2012-11-03T22:34:45Z Indexed on 2012/11/03 23:00 UTC
Read the original article Hit count: 808

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:

example of single form rendered with crispy-forms

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?

screenshot of django-admin

© Stack Overflow or respective owner

Related posts about django

Related posts about django-forms