Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django form EmailField doesn't accept the css attribute

I have a from containing some fields, but my css class applies to all the fileds except the EmailField. I've also tried sender.widget.attrs.update({'class':"contatct-form"}) and it still doesn't work (just change the size of field). Does anybody knows what the problem is? as all of my searches were unsuccessful.

form:

from django import forms
class NameForm(forms.Form):
     your_name = forms.CharField(initial='Your name', max_length=100)
     sender = forms.EmailField()
     #sender.widget.attrs.update({'class':"contatct-form"})
     message = forms.CharField(widget=forms.Textarea)

template:

        <div class="contatct-form">
            <form action="" method="post">
                {% csrf_token %}
                {{ form }}
                <input type="submit" value="send" />
            </form>
        </div>
like image 217
Mazdak Avatar asked Dec 12 '22 00:12

Mazdak


1 Answers

The problem you have probably is because you have not assigned any widget to your EmailField(), change to this (like @Todor said) should work:

...
sender = forms.EmailField(
    widget=forms.EmailInput(attrs={'class': 'contatct-form'})
) 

If this doesn't work for whatever reason (probably wrong css styling), you can just change the styling in your css/class directly like so:

div.contatct-form form input[type=email] {
    /* your changes here... */
}

Hope this helps.

like image 157
Anzel Avatar answered Feb 03 '23 18:02

Anzel