Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 如何创建不允许编辑某些字段的自定义UserChangeForm?_Django_Django Models_Django Admin_Django Forms - Fatal编程技术网

Django 如何创建不允许编辑某些字段的自定义UserChangeForm?

Django 如何创建不允许编辑某些字段的自定义UserChangeForm?,django,django-models,django-admin,django-forms,Django,Django Models,Django Admin,Django Forms,这是我在SO上的第二篇文章,也是我在Django上的第二篇文章——请温柔一点 我正在研究一个简单的用户模型。用户应该能够注册该网站,并且在注册后,他们应该能够在登录时更改其帐户设置。但是,如果我执行以下操作,用户会看到一个巨大的表单,其中包含我不希望他们编辑的各种信息: views.py 似乎这还不够糟糕,此表单不允许进行任何更改,引用“具有此用户名的用户已存在”作为错误。这让我很困惑——我正试图保存一个UserChangeForm,那么它不是已经存在了吗 我已经在网上找了一段时间了,根据我在上

这是我在SO上的第二篇文章,也是我在Django上的第二篇文章——请温柔一点

我正在研究一个简单的用户模型。用户应该能够注册该网站,并且在注册后,他们应该能够在登录时更改其帐户设置。但是,如果我执行以下操作,用户会看到一个巨大的表单,其中包含我不希望他们编辑的各种信息:

views.py 似乎这还不够糟糕,此表单不允许进行任何更改,引用“具有此用户名的用户已存在”作为错误。这让我很困惑——我正试图保存一个UserChangeForm,那么它不是已经存在了吗

我已经在网上找了一段时间了,根据我在上面看到的内容,尝试制作自己的自定义表单,比如:

forms.py 不幸的是,这还没有成功。我有点不知所措,所以任何帮助都将不胜感激。以下是我的URL.py和模板:

url.py 模板
{%extends“accounts/base.html”%}
{%block main%}
欢迎,{request.user.pk}{{request.user.username}}访问accounts.templates.accounts.settings.html。
在这里您可以更新您的用户设置。您可以更改密码。
{%csrf_令牌%}
{{form.as_p}}

{%endblock%}

提前感谢您的帮助

看起来您没有仔细阅读文档

让我来帮你,退房


基本上,您只需要将
exclude=('fieldname',)
添加到您的modelforms元中。

首先:要限制表单中的字段,请遵循。过滤字段基本上有两个选项:
字段
排除

第二:你总是在创建一个新用户。使用用户实例初始化表单,不仅可以保存表单,还可以创建初始数据集

您的视图代码应为:

# in the POST:
form = UserChangeForm(request.POST, instance=request.user)

# otherwise
form = UserChangeForm(instance=request.user)

并从
form.py
中删除值赋值。首先,应该将self.fields['username']设置为表单字段,如forms.CharField(initial=self.instance.username)。其次,在视图中使用的是UserChangeForm而不是CustomUserChangeForm。第三,(现在是一个正确的链接,而不是一个到某人硬盘的链接…

这需要更新:您不能排除UserChangeForm()中的字段


太糟糕了。

我的工作方式是添加一个表单类(在forms.py中)覆盖UserChangeForm:

from django.contrib.auth.forms import UserChangeForm    

class UserChangeForm(UserChangeForm):
"""Overriding visible fields."""
    class Meta:
        model = User
        fields = ('username', 'password', 'email', 'first_name', 'last_name',)

最后在views.py上导入这个版本,而不是contrib.auth.forms

我想我已经晚了,但为了将来的读者,我还是会发布我的文章

class UserUpdateForm(UserChangeForm):
    password = None

    class Meta:
        model = CustomUser
        fields = ['username', 'email', 'first_name', 'last_name', 'description', ]
        widgets = {
            'description': forms.Textarea(attrs={'rows': 3}),
            'username': forms.TextInput(attrs={'readonly': 'readonly'}),
            'email': forms.TextInput(attrs={'readonly': 'readonly'})
        }

这将创建一个自定义表单,其中
用户名
电子邮件
字段为只读字段,因为您不希望用户在创建用户帐户后更改这些唯一字段。它还阻止显示
密码
字段,因为在配置文件页面上显示hased
密码
没有意义

我还面临一个排除密码字段的问题,这就是我在这里登陆的原因

在检查了一些好项目的代码后,我找到了一种排除密码字段的方法(不使用exclude)

只需输入如下代码所示的
password=None

from django.contrib.auth.forms import UserChangeForm    

class CustomUserChangeForm(UserChangeForm):

    # make the password as None for removing the password field from the form
    password = None

    class Meta:
        model = User

        # select the fields that you want to display
        fields = ('email', 'first_name', 'last_name',)

谢谢你的帮助!我应该更彻底地检查文档,对此我很抱歉。根据你的建议,我将表单更改为以User为例。但是,似乎在继承UserChangeForm时,我不能
排除
User.username。无论我是在Meta中设置字段,还是专门排除它用户名字段出现在表单上。我想知道它是否与此有关。你的想法?我不久前也遇到了同样的问题…看起来类链中的第一个
Meta
类定义没有覆盖,这似乎不是真的。我发现你显然不能排除
username
>密码
既不通过
排除
也不通过
排除字段
。关于如何实现这一点,您有什么想法吗?您不能为UserChange排除字段。我真的很尴尬,我错过了专门解决我问题的文档--谢谢您的帮助!
# in the POST:
form = UserChangeForm(request.POST, instance=request.user)

# otherwise
form = UserChangeForm(instance=request.user)
from django.contrib.auth.forms import UserChangeForm    

class UserChangeForm(UserChangeForm):
"""Overriding visible fields."""
    class Meta:
        model = User
        fields = ('username', 'password', 'email', 'first_name', 'last_name',)
class UserUpdateForm(UserChangeForm):
    password = None

    class Meta:
        model = CustomUser
        fields = ['username', 'email', 'first_name', 'last_name', 'description', ]
        widgets = {
            'description': forms.Textarea(attrs={'rows': 3}),
            'username': forms.TextInput(attrs={'readonly': 'readonly'}),
            'email': forms.TextInput(attrs={'readonly': 'readonly'})
        }
from django.contrib.auth.forms import UserChangeForm    

class CustomUserChangeForm(UserChangeForm):

    # make the password as None for removing the password field from the form
    password = None

    class Meta:
        model = User

        # select the fields that you want to display
        fields = ('email', 'first_name', 'last_name',)