0

I've created a form in Django using crispy forms, it works fine but I can tell just by looking at it that I've most likely over engineered it. It's a lot of very similar pieces of code.

How should I actually be doing this? I would presume there's a much better way.

class New_Contact(forms.Form):

    title = forms.CharField(label=False, max_length=10,required=False)
    givenName = forms.CharField(label=False, max_length=255)
    middleName = forms.CharField(label=False, max_length=255,required=False)
    surname = forms.CharField(label=False, max_length=255)
    jobTitle = forms.CharField(label=False, max_length=255,required=False)
    companyName = forms.CharField(label=False, max_length=255,required=False)
    department = forms.CharField(label=False, max_length=255,required=False)
    businessHomePage = forms.CharField(label=False, max_length=255,required=False)
    assistantName = forms.CharField(label=False, max_length=255,required=False)
    homePhones = forms.CharField(label=False, max_length=255,required=False) # phones
    mobilePhone = forms.CharField(label=False, max_length=255,required=False) # phones
    businessPhones1 = forms.CharField(label=False, max_length=255,required=False) # phones
    businessPhones2 = forms.CharField(label=False, max_length=255,required=False) # phones
    homeAddress = forms.CharField(label=False,required=False,widget=forms.Textarea(attrs={'style': 'height: 8em'})) # addresses
    businessAddress = forms.CharField(label=False,required=False,widget=forms.Textarea(attrs={'style': 'height: 8em'})) # addresses
    emailAddresses1 = forms.CharField(label=False, max_length=255,required=False) # mail
    emailAddresses2 = forms.CharField(label=False, max_length=255,required=False) # mail

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.fields['title'].widget.attrs['placeholder'] = 'Title'
        self.fields['givenName'].widget.attrs['placeholder'] = 'First Name'
        self.fields['middleName'].widget.attrs['placeholder'] = 'Middle Name'
        self.fields['surname'].widget.attrs['placeholder'] = 'Last Name'
        self.fields['mobilePhone'].widget.attrs['placeholder'] = 'Mobile'
        self.fields['homePhones'].widget.attrs['placeholder'] = 'Home Phone'
        self.fields['emailAddresses1'].widget.attrs['placeholder'] = 'Email'
        self.fields['emailAddresses2'].widget.attrs['placeholder'] = 'Email'
        self.fields['homeAddress'].widget.attrs['placeholder'] = 'Address'
        self.fields['companyName'].widget.attrs['placeholder'] = 'Company Name'
        self.fields['department'].widget.attrs['placeholder'] = 'Department'
        self.fields['jobTitle'].widget.attrs['placeholder'] = 'Job Title'
        self.fields['assistantName'].widget.attrs['placeholder'] = 'Assistant Name'
        self.fields['businessHomePage'].widget.attrs['placeholder'] = 'Website'
        self.fields['businessPhones1'].widget.attrs['placeholder'] = 'Phone'
        self.fields['businessPhones2'].widget.attrs['placeholder'] = 'Phone'
        self.fields['businessAddress'].widget.attrs['placeholder'] = 'Address'
        self.helper.layout = Layout(
            HTML("""
                <h3 class="py-2">Personal Details</h3>           
                """),
            Row(
                Field('title', wrapper_class='col-sm-3'),
                Field('givenName', wrapper_class='col-sm-5'),
                Field('middleName', wrapper_class='col-sm-4'),
                Field('surname', wrapper_class='col-sm-12'),
                Field('mobilePhone', wrapper_class='col-sm-6'),
                Field('homePhones', wrapper_class='col-sm-6'),
                Field('emailAddresses1', wrapper_class='col-sm-12'),
                Field('homeAddress', wrapper_class='col-sm-12'),
                ),
            HTML("""
                <h3 class="py-2">Company Details</h3>           
                """),
            Row(
                Field('companyName', wrapper_class='col-sm-12'),
                Field('department', wrapper_class='col-sm-6'),
                Field('jobTitle', wrapper_class='col-sm-6'),
                Field('assistantName', wrapper_class='col-sm-12'),
                Field('businessHomePage', wrapper_class='col-sm-12'),
                Field('businessPhones1', wrapper_class='col-sm-6'),
                Field('businessPhones2', wrapper_class='col-sm-6'),
                Field('emailAdresses2', wrapper_class='col-sm-12'),
                Field('businessAddress', wrapper_class='col-sm-12'),
                ),
            )

The main part that really adds to the code is having to have a line for every placeholder. It just looks a bit of a mess, though I accept it's easier than just writing out all the html.

Thanks!

scottapotamus
  • 548
  • 3
  • 18
  • from the form it seems like you adding placeholder and class attributes to each fields, am i right ? – Linh Nguyen Oct 29 '19 at 03:51
  • yes that's right – scottapotamus Oct 29 '19 at 04:11
  • 1
    you can add html attributes to the forms field itself [add atrrs to django form](https://stackoverflow.com/a/2902055/11225821) – Linh Nguyen Oct 29 '19 at 04:17
  • I've managed to remove the need for the placeholder section by using widget=forms.TextInput(attrs={'placeholder':'Title'}) – scottapotamus Oct 29 '19 at 04:51
  • 1
    you can add the class to the field too btw, you can add any html attributes to the fields using the the widget, since the form just gonna render html to the template anyway. you can add use multiple attributes like so `attrs={'class': 'col-sm-12','placeholder':'Title''}` – Linh Nguyen Oct 29 '19 at 04:54
  • Yep I've done that now, all waaaayyy tidier. Thanks for your help. – scottapotamus Oct 29 '19 at 06:07

0 Answers0