开发者

How can I override the django AuthenticationForm input css class?

开发者 https://www.devze.com 2022-12-21 02:58 出处:网络
I have a django site using the basic django registration framework. I have my login page working fine, but I want to change the css class on the inputs. The开发者_如何学JAVA form passed to the login p

I have a django site using the basic django registration framework. I have my login page working fine, but I want to change the css class on the inputs. The开发者_如何学JAVA form passed to the login page looks to be an AuthenticationForm class.

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


Sub class your auth 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'}))


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))
0

精彩评论

暂无评论...
验证码 换一张
取 消