Jake Rankin
Jake Rankin

Reputation: 744

How to add style in django form definition for extended forms that don't correspond to model?

I have a django form and I am using form to process orders and card payments. I have extended my form in my view to have credit card fields, as these fields do not correspond to my model, as I am not saving them in my database.

In my forms.py, I am specifying the class attribute for my form fields like so:

class OrderForm(forms.ModelForm):
    class Meta:
        model = Order
        fields = ['first_name', 'last_name', 'email',
        'address', 'zipcode', 'city', 'state']
        widgets = {
        'first_name': forms.TextInput(attrs={'class': 'sizefull s-text7 p-l-22 p-r-22', 'placeholder': 'First Name'}),
        'last_name': forms.TextInput(attrs={'class': 'sizefull s-text7 p-l-22 p-r-22', 'placeholder': 'Last Name'}),
        'email': forms.TextInput(attrs={'class': 'sizefull s-text7 p-l-22 p-r-22', 'placeholder': 'E-Mail'}),
        'address': forms.TextInput(attrs={'class': 'sizefull s-text7 p-l-22 p-r-22', 'placeholder': 'Address'}),
        'zipcode': forms.TextInput(attrs={'class': 'sizefull s-text7 p-l-22 p-r-22', 'placeholder': 'Zipcode'}),
        'city': forms.TextInput(attrs={'class': 'sizefull s-text7 p-l-22 p-r-22', 'placeholder': 'City'}),
        'state': forms.TextInput(attrs={'class': 'sizefull s-text7 p-l-22 p-r-22', 'placeholder': 'State'}),
        }

I understand it is frowned upon to mix presentation and business logic, however I have a strict deadline, and I am working with a paid template with advanced CSS that I do not entirely understand, so this is the method I have found to work so far. I will revise this at a later stage to add the class via a template tag.

However, as this is working, I would like to continue to use this method.

My issue is that I have been unable to assign attributes in the same way to my extended form in my view, which is as follows:

class OrderPayForm(OrderForm):

    card_num = forms.CharField(max_length=16)
    exp_month = forms.CharField(max_length=2)
    exp_year = forms.CharField(max_length=2)
    cvc = forms.CharField(max_length=3)

    class Meta(OrderForm.Meta):
        fields = OrderForm.Meta.fields + ['card_num', 'exp_month', 'exp_year', 'cvc']

Attempting to add attributes either in my forms.py or in my views.py like:

class OrderPayForm(OrderForm):

    card_num = forms.CharField(max_length=16)
    exp_month = forms.CharField(max_length=2)
    exp_year = forms.CharField(max_length=2)
    cvc = forms.CharField(max_length=3)

    class Meta(OrderForm.Meta):
        fields = OrderForm.Meta.fields + ['card_num', 'exp_month', 'exp_year', 'cvc']

        widgets = {
        'card_num': forms.TextInput(attrs={'class': 'sizefull s-text7 p-l-22 p-r-22', 'placeholder': 'Card Number'}),
        }

This does not result in an error message, but results in all of my formfields losing the class attributed assigned to in my forms.py.

What am I doing wrong?

Upvotes: 0

Views: 54

Answers (1)

p14z
p14z

Reputation: 1810

When you define widgets in the subclass you override the widget definition in the parent class, what you can do to avoid this is writing a custom initialization method:

class OrderPayForm(OrderForm):
    card_num = forms.CharField(max_length=16)
    exp_month = forms.CharField(max_length=2)
    exp_year = forms.CharField(max_length=2)
    cvc = forms.CharField(max_length=3)

    class Meta(OrderForm.Meta):
        fields = OrderForm.Meta.fields + ['card_num', 'exp_month', 'exp_year', 'cvc']


    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['card_num'].widget = forms.TextInput(attrs={
            'class': 'sizefull s-text7 p-l-22 p-r-22',
            'placeholder': 'Card Number'
        })

Upvotes: 1

Related Questions