Search code examples
djangodjango-modelsdjango-formsdjango-crispy-forms

Django {% crispy %} tag vs {{ form|crispy }} filter what's the difference?


I'm trying to wrap my head around what crispy forms is doing in the background.

When I put the tag {% crispy form %} into my HTML block, my form layouts and crispy bootstrap formatting (from crispy_forms.layout import Layout, Row, Column and from crispy_forms.bootstrap import AppendedText, InlineRadios ) is rendered properly but the submit button does not post to my model or redirect the user.

When I put the tag {{ form|crispy }} into my HTML block, my form layout is not rendered but the submit button does work and posts the user input to my model.

I'm trying to figure out how to get both a nice layout and have a functional HTML form.


Solution

  • I have figured it out,

    You have to add the POST button to your helper in Python in the model/form for the {% crispy form %} tag to work.

    I originally had a submit button in HTML <button class="btn btn-primary" type="submit">Submit</button> which only worked with the crispy filter {{ form|crispy }}

    class ProfileUpdateForm(forms.ModelForm):
        class Meta:
            model = Profile
    
        def __init__(self, *args, **kwargs):
            super(ProfileUpdateForm, self).__init__(*args, **kwargs)
            self.helper = FormHelper(self)
            self.helper.layout = Layout(AppendedText('foo','bar')
            self.helper = FormHelper(self)
            self.helper.add_input(Submit('submit', 'Submit', css_class='btn-primary'))
            self.helper.form_method = 'POST'