How can I override the django AuthenticationForm I / O css class?

I have a django site using the basic django registration structure. My login page is working fine, but I want to change the css class to logins. The form submitted to the login page looks like an AuthenticationForm class.

What would be a good way to add a css class to the username and password fields?

+6
authentication django forms
source share
2 answers

I do what you want like this:

def login(request): form = AuthenticationForm(request) form.fields['username'].widget.attrs['class'] = "custom_css" form.fields['password'].widget.attrs['style'] = "background:red" return render_to_response("login.html", {'form':form}, context_instance=RequestContext(request)) 
+4
source share

Subclass for your form

forms.py

 from django import forms from django.contrib.auth.forms import AuthenticationForm from django.forms.widgets import PasswordInput, TextInput class RFPAuthForm(AuthenticationForm): username = forms.CharField(widget=TextInput(attrs={'class': 'span2','placeholder': 'Email'})) password = forms.CharField(widget=PasswordInput(attrs={'class': 'span2','placeholder':'Password'})) 
+7
source share

All Articles