我正在使用 django 内置登录forms我想在用户名和密码中添加占位符。
我的模板:
<div class="form-group">
<div class="col-md-12">
{{ form.username|add_class:'form-control' }}
</div>
</div>
<div class="form-group">
<div class="col-md-12">
{{ form.password|add_class:'form-control' }}
</div>
</div>
我怎样才能做到这一点?
将此内容保存在forms.py
from django import forms
from django.contrib.auth.forms import AuthenticationForm
from django.forms.widgets import PasswordInput, TextInput
class CustomAuthForm(AuthenticationForm):
username = forms.CharField(widget=TextInput(attrs={'class':'validate','placeholder': 'Email'}))
password = forms.CharField(widget=PasswordInput(attrs={'placeholder':'Password'}))
在你的主要urls.py
(您的登录视图在其中调用)
from django.contrib.auth import views as auth_views
from app.forms import CustomAuthForm
urlpatterns = [
url(r'^login/$', auth_views.login, name='login', kwargs={"authentication_form":CustomAuthForm}),
]
我们在这里做的额外事情是添加一个 kwargskwargs={"authentication_form":CustomAuthForm}
请将此作为您将来的参考django.contrib.auth.views.LoginView and django.contrib.auth.forms.AuthenticationForm
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)