Django将占位符添加到内置登录表单的django中 [英] Django adding placeholders to django built in login forms

查看:101
本文介绍了Django将占位符添加到内置登录表单的django中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django内置登录表单,我想在用户名和密码中添加占位符。



我的模板:

 < 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

 从django导入表单
从django.contrib.auth.forms导入AuthenticationForm
从django.forms.widgets导入PasswordInput,TextInput


类CustomAuthForm (AuthenticationForm):
用户名= form.CharField(widget = TextInput(attrs = {'class':'validate','placeholder':'Email'})))
password = form.CharField(widget = PasswordInput(attrs = {'placeholder':'Password'}))

在您的主 urls.py (在您的登录视图所在的位置)



来自django.contrib。 auth从app.forms导入为auth_views
的视图import CustomAuthForm

urlpatterns = [
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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆