【问题标题】:Django Allauth replace labelsDjango Allauth 替换标签
【发布时间】:2018-02-01 15:11:38
【问题描述】:

我有ACCOUNT_SIGNUP_FORM_CLASS指向的以下表格

class SignupForm(forms.Form):

    def __init__(self, *args, **kwargs):
        super(SignupForm, self).__init__(*args, **kwargs)
        for field_name in self.fields.keys():
            print(field_name, self.fields[field_name].label)

我正在尝试替换以下每个字段的标签:usernameemailpassword1password2。启动表单时,将打印以下内容。

   username Username
   email None

但是在我的浏览器中呈现的表单显示了所有带有标签的字段

Username*
E-mail*
Password*
Password (again)*

为什么只有usernameemail 被打印,为什么email 字段标签是None 但它在渲染时显示得很好。

如何更改所有四个字段的标签?

【问题讨论】:

  • print(dir(self.fields['email'])) 的输出是什么?

标签: django python-3.x django-allauth


【解决方案1】:

您不能在ACCOUNT_SIGNUP_FORM_CLASS 上设置的表单上执行此操作。该表单用作来自 allauth 的 SignupForm 的基类。因此,您不能更改任何字段,因为它在该表单上不存在(仅在子类上)。要做你想做的事,你必须从 django allauth 继承 SignupForm 并将其用作真正的注册表单,可以这样设置:

# On settings
ACCOUNT_FORMS = {'signup': 'path.to.custom.singup.form'}

自定义注册表单必须继承自 django allauth singup 表单,如下所示:

from allauth.account.forms import SignupForm    


class CustomSignupForm(SignupForm):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # here you can change the fields
        self.fields['email'] = forms.EmailField(label='custom label')

【讨论】:

    猜你喜欢
    • 2020-09-08
    • 2021-08-31
    • 2010-12-12
    • 2011-12-22
    • 1970-01-01
    • 2016-06-17
    • 2018-12-30
    • 2013-12-10
    • 2016-07-26
    相关资源
    最近更新 更多