【问题标题】:Django form password TypeError: __init__() got an unexpected keyword argument 'widget'Django 表单密码 TypeError: __init__() got an unexpected keyword argument 'widget'
【发布时间】:2020-10-24 18:12:17
【问题描述】:

我的 models.py 中有这段代码:

from django import forms

class user_Account(models.Model):
    password = models.CharField(
        widget=forms.PasswordInput(),
        verbose_name="Password",
        max_length=500,
        null=True,
        blank=True
    )

这是我的forms.py:

from django import forms
from Account.models import user_Account

class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)
    class Meta:
        model = user_Account

为什么会出现这个错误?

TypeError: __init__() got an unexpected keyword argument 'widget'

我错过了什么吗?

【问题讨论】:

    标签: python django


    【解决方案1】:

    您不能在模型列中设置小部件。因为小部件是表单字段的关键字参数。

    class user_Account(models.Model):
        password = models.CharField(verbose_name="Password", max_length=500, null=True, blank=True)
    

    form.py

    from django import forms
    from Account.models import user_Account
    class UserForm(forms.ModelForm):
        password = forms.CharField(widget=forms.PasswordInput())
        class Meta:
            model = user_Account
    

    【讨论】:

    • 我该怎么办?啊,你能发布你的答案吗?
    • 请稍等
    • 你能看看我的新帖子吗?我只是删除了 models.py 中的小部件参数
    猜你喜欢
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 2021-06-11
    • 2020-11-16
    相关资源
    最近更新 更多