Search code examples
pythondjangodjango-formsdjango-templates

Django - Can a crispy form be split into 2 columns?


I'm trying to split a crispy layout form into 2 columns to try and eradicate the need to scroll, I've tried to use formhelper in forms.py to put 2 questions in a DIV but that doesn't change anything. Does anyone have any ideas?

forms.py

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Div, Field, Row, Submit, Button, Column

class ProfileUpdateForm(forms.ModelForm):
    address = forms.CharField()
    dob = forms.DateField(
        widget=forms.TextInput(
            attrs={'type': 'date'}
        ), label='Date of Birth'
    )
    def __init__(self, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Div(
                Div(Field('fullname'), css_class='col-md-6',),
                Div(Field('dob'), css_class='col-md-6',),
                css_class='row',
            ),
            Div(
                Div(Field('address'), css_class='col-md-6',),
                Div(Field('city'), css_class='col-md-6',),
                css_class='row',
            ),
            Div(
                Div(Field('country'), css_class='col-md-6',),
                Div(Field('profilephoto'), css_class='col-md-6',),
                css_class='row',
            ),
        )
        super(ProfileUpdateForm, self).__init__(*args, **kwargs)
    class Meta:
        model = Profile
        fields = ['fullname', 'dob', 'address', 'city', 'country', 'profilephoto']
        labels = {
            'fullname': 'Full Name',
            'address': 'Address',
            'city': 'City',
            'country': 'Country',
            'profilephoto': 'Profile Photo',
        }

views.py

@login_required
def profile(request):
    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)

        if u_form.is_valid and p_form.is_valid:
            u_form.save()
            p_form.save()
            messages.success(request, f'Your account has been successfully updated!')
            return redirect('profile')
    else:   
        u_form = UserUpdateForm(instance = request.user)
        p_form = ProfileUpdateForm(instance = request.user.profile)

    context = {
        'u_form': u_form,
        'p_form': p_form
    }
    return render(request, 'users/profile.html', context)

profile.html

{% load crispy_forms_tags %}

            <form method="POST" enctype="multipart/form-data">
                {% csrf_token %}
                {{ u_form|crispy }}

                {{ p_form|crispy }}

           </form>

Current State

Current look of the page

EDIT Added __init__ to forms.py


Solution

  • Actually, I'm not even sure if your helper is picked up. Based on the documentation here the formhelper should be added at __init__. And your issue could also be in your template if you don't use {% crispy your_cool_form_name %} there.

    You're almost there. You're looking for Field.

    from crispy_forms.layout import Field
    
    [...]
    
    
    Div(
        Div(Field('fullname'), css_class='col-md-6',),
        Div(Field('dob'), css_class='col-md-6',),
        css_class='row',
    ),