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?
Installation of django-crispy-forms is quite standard. You will need to use a package manager "pip" or "easy_install", then add an installed application 'crispy_forms' to the INSTALLED_APPS list. If you plan on using Bootstrap (or any other CSS framework), then you will also have to import it to the project.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With