Django 添加占位符到 django 内置登录表单

问题描述 投票:0回答:6

我正在使用 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>

我该怎么做?

python django django-forms
6个回答
14
投票

将此内容保存在

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}),
]

我们在这里做的额外事情是添加了 kwargs

kwargs={"authentication_form":CustomAuthForm}

请将此作为您将来的参考 django.contrib.auth.views.LoginViewdjango.contrib.auth.forms.AuthenticationForm


6
投票

将此内容保存在

forms.py

from django import forms
from django.contrib.auth.forms import AuthenticationForm
from django.forms.widgets import PasswordInput, TextInput


class MyAuthForm(AuthenticationForm):
    class Meta:
        model = User
        fields = ['username','password']
    def __init__(self, *args, **kwargs):
        super(MyAuthForm, self).__init__(*args, **kwargs)
        self.fields['username'].widget = forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Username'})
        self.fields['username'].label = False
        self.fields['password'].widget = forms.PasswordInput(attrs={'class': 'form-control', 'placeholder':'Password'}) 
        self.fields['password'].label = False

并将此内容保存在主目录中

urls.py

from users.forms import MyAuthForm

urlpatterns = [
   ...
   path('', auth_views.LoginView.as_view(template_name='users/login.html', authentication_form=MyAuthForm), name='login'),
   ...
]

请参考此网站:https://github.com/django/django/blob/master/django/contrib/auth/views.py


1
投票

您可以添加另一个过滤器

@register.filter(name="add_place_holder")
def add_place_holder(field, place_holder=None):
    if place_holder == None:
        return field
    else:
        field.field.widget.attrs.update({'placeholder': place_holder})
        return field

0
投票

是的,你可以添加这样的占位符,

class LoginForm(forms.ModelForm):
    class Meta:
        model = YourModelName
        widgets = {
            'username' : forms.TextInput(attrs = {'placeholder': 'Username'}),
            'password' : forms.PasswordInput(attrs = {'placeholder': 'Password'}),
        }

我希望这对你有用。


0
投票

回应您对其他答案的评论。您可以对身份验证表单进行子类化。

from django import forms
from django.contrib.auth.forms import AuthenticationForm
from django.forms.widgets import PasswordInput, TextInput

class PlaceholderAuthForm(AuthenticationForm):
    username = forms.CharField(widget=TextInput(attrs={'placeholder': 'Email'}))
    password = forms.CharField(widget=PasswordInput(attrs={'placeholder':'Password'}))

或者查看这个问题中提出的其他函数。 --> 如何覆盖 django AuthenticationForm 输入 css 类?


0
投票

我所做的是使用 Django.contrib.auth 中的 LoginView

以我的形式.py

class LoginForm(AuthenticationForm)
    password = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Password'}))

class Meta:
    model = CustomUser
    fields = ['username', 'password']
    
}

然后在我的应用程序 URL 中

from django.contrib.auth import views as auth_views
path('{app_name}/login/', auth_views.LoginView.as_view(template_name="registration/login.html",
                                                    authentication_form=LoginForm)
© www.soinside.com 2019 - 2024. All rights reserved.