sidestrand
sidestrand

Reputation: 71

crispy forms bootstrap 3 submit problems

This has really stumped me. I can write forms OK in django but want to use crispy-forms bootstrap3. I can get the forms to render using this form code:

class NewGuestForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(NewGuestForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        self.helper.form_id = 'id-newGuestForm'
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-lg-2'
        self.helper.field_class = 'col-lg-6'
        self.helper.form_method = 'post'
        self.helper.form_action = 'guest-list'
        self.helper.layout = Layout(
            Fieldset (
                'New Guest',
                'first_name',
                'last_name',
                'num_child',
                'landline',
                'mobile',
                'email'
                ),
            FormActions(
                Submit('save', 'Save changes',css_class='btn-primary'),
                Button('cancel', 'Cancel')
            )
        )

    class Meta:
        model = Guest

class BookingForm(forms.Form): class Meta: model = Booking

    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.form_method = 'POST'
        self.helper.form_id = 'add-booking'
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-lg-2'
        self.helper.field_class = 'col-lg-6'
        self.helper.layout = Layout(
            Fieldset(
                '',
                'guest',
                'guest_status',
                'start_date',
                'end_date',
                'dep_recd',
                'bkd_child',
                'bkd_adult',
                'bal_recd',
                'sec_recd',
                'keys_sent',
                'sec_retn',
                'notes'
                ),
        FormActions(
            Submit('submit', "Save changes"),
            Submit('cancel', "Cancel"),
            )       
        )

        super(BookingForm, self).__init__(*args, **kwargs)

This renders the form OK, but when I click 'submit' the browser goes white. The return (form_action) is correctly shown in the address bar, but isn't loaded. The data is not written to the database. The form renders with only the fields I need.

My view is:

class NewGuestView(CreateView):
    model = Guest
    template_name = 'new_guest.html'
    form_class = NewGuestForm

If I change 'form_class' to 'form' the form renders with all fields and ignores the bootstrap column instructions. Also, the 'submit' and 'cancel' buttons do not appear.

I must be doing something wrong, but can't for the life of me see what. Any suggestions gratefully received.

Upvotes: 2

Views: 1486

Answers (2)

sidestrand
sidestrand

Reputation: 71

Resolved now. I had not realised that I needed to add the fields to the class Meta as well as listing them in Layout. I added the following

    class Meta:
    model = Guest
    fields = ['first_name', 'last_name', 'email', 'landline', 'mobile']

Now the form saves OK.

Upvotes: 1

danj1974
danj1974

Reputation: 481

This is probably a bit late for you, but I've been looking at django-crispy-forms for the first time today and ran into the same problem as you. If I have the form_action defined, then upon submission of the form the browser is redirected to the correct url, but the page is blank - even upon refreshing. This also happens whether the form is valid or not, so clearly this is a pretty fundamental problem and there must be something we're both doing wrong.

I got around this by using the success_url attribute of the view. So you could have tried:

from django.core.urlresolvers import reverse_lazy

class NewGuestView(CreateView):
    ...
    success_url = reverse_lazy("guest-list")

As for the buttons, I haven't yet gotten to defining the layout and have used this approach:

    self.helper.add_input(Submit('submit', 'Submit'))
    self.helper.add_input(Button('cancel', 'Cancel'))

Although it's worth noting that the 'Cancel' button doesn't actually do anything at this stage, I'll need to look into that further.

Did you manage to get this working or find another way around?

Update:

The redirect fails with a 405 Method Not Allowed error. I tried defining the post() method in my view as per this SO question and this solves the HTTP error, but doesn't process the data (a new record isn't saved and validation errors are not caught). So I'm sticking with the success_url method until I can find out what I'm doing wrong.

Upvotes: 1

Related Questions