Django 将占位符添加到 django 内置的登录表单 [英] Django adding placeholders to django built in login forms
问题描述
我正在使用 django 内置登录 表单,我想为用户名和密码添加占位符.
我的模板:
<div class="col-md-12">{{ form.username|add_class:'form-control' }}
<div class="form-group"><div class="col-md-12">{{ form.password|add_class:'form-control' }}
我该怎么做?
将此内容保存在 forms.py
from django 导入表单从 django.contrib.auth.forms 导入 AuthenticationForm从 django.forms.widgets 导入 PasswordInput, TextInput类 CustomAuthForm(AuthenticationForm):用户名 = forms.CharField(widget=TextInput(attrs={'class':'validate','placeholder':'Email'}))密码 = forms.CharField(widget=PasswordInput(attrs={'placeholder':'Password'}))
在您的主要 urls.py
(您的登录视图调用的位置)
from django.contrib.auth 将视图导入为 auth_views从 app.forms 导入 CustomAuthForm网址模式 = [url(r'^login/$', auth_views.login, name='login', kwargs={"authentication_form":CustomAuthForm}),]
我们在这里做的额外事情是添加了一个 kwargs kwargs={"authentication_form":CustomAuthForm}
请使用此作为您将来的参考django.contrib.auth.views.LoginView 和 django.contrib.auth.forms.AuthenticationForm
I'm using django built-in login forms and i want to add placeholders to username and password.
My template:
<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>
How can i do this?
save this content in 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'}))
in your main urls.py
(where your login view called)
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}),
]
the extra thing that we done here is added an kwargs kwargs={"authentication_form":CustomAuthForm}
please use this for your future reference django.contrib.auth.views.LoginView and django.contrib.auth.forms.AuthenticationForm
这篇关于Django 将占位符添加到 django 内置的登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!