有什么办法可以在用户身份验证登录页面中更改用户名字段标签?

时间:2018-12-30 19:08:50

标签: django django-templates django-login

Right now login page looked like this

我想将用户名字段的标签更改为Team Name

注意:我正在使用内置的LoginView

2 个答案:

答案 0 :(得分:2)

根据documentation LoginView具有一个称为authentication_form的属性(通常只是一个表单类)。默认为AuthenticationForm

您可以创建一个继承自AuthenticationForm的表单类,设置用户名字段的标签,并将其分配给LoginView属性上的authentication_form

forms.py

from django import forms    
from django.contrib.auth.forms import AuthenticationForm, UsernameField


class CustomAuthenticationForm(AuthenticationForm):
    username = UsernameField(
        label='Team Name',
        widget=forms.TextInput(attrs={'autofocus': True})
    )

views.py

from django.contrib.auth.views import LoginView

from .forms import CustomAuthenticationForm


class CustomLoginView(LoginView):
    authentication_form = CustomAuthenticationForm

答案 1 :(得分:0)

只需更改用户名字段的标签文本,如下所示:

 class LoginForm(ModelForm):
    class Meta:
        model = YourModel
        fields = ['username','password']

    def __init__(self, *args, **kwargs):
        super(LoginForm, self).__init__(*args, **kwargs)

        for fieldname in ['username']:
            self.fields[fieldname].label = 'Email'