管理 UserCreationForm 属性和字段

发布于 2024-12-22 17:39:31 字数 303 浏览 0 评论 0原文

我是 Django 的新手,即使在彻底尝试找到这个问题的答案之后,我也找不到任何答案。 =/

我正在使用 UserCretionForm 创建我的用户,我想知道一些事情:

1 - 如何管理此表单的属性? (例如,我不想显示“必需。30 个字符...”或“输入与上面相同的密码,以进行验证。”之类的提示。)

2 - 我想让它显示其他自定义字段。我该怎么做呢? (请尽量弄清楚我必须在哪里编写您将向我展示的代码,因为我不是专家=D)。 (例如,在巴西,我们需要存储一些国家信息。这就是我需要这些字段的原因。)

提前致谢! (是)

I am new in Django and even after trying thoroughly to find an answer for this question, I couldn't find any. =/

I am using UserCretionForm to create my users and I wanted to know a couple of things:

1 - How can I manage this form's properties? (e.g. I don't want to show the tips like "Required. 30 charact..." or "Enter the same password as above, for verification.")

2 - I want to make it show other customized fields. How can I do it? (please, try to be clear where I have to write the codes you'll show me as I am not an expert =D ). (e.g. Here in Brazil we have some national info I need to store. That is why I need these fields.)

Thanks in advance! (Y)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我纯我任性 2024-12-29 17:39:31

更改默认验证器消息

您可以通过表单字段的 error_messages 参数更改默认验证器的错误消息。

要了解每个字段存在哪些验证器,请检查此处:https:/ /docs.djangoproject.com/en/dev/ref/forms/fields/#built-in-field-classes

class MyForm(UserCreationForm):
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields['username'].error_messages = {'invalid': 'foobar'}
        self.fields['password1'].error_messages = {'required': 'required, man'}

向现有表单添加新字段

如果要添加新字段,可以通过以下方式添加它们子类化(这只是Python)。

如果您继承 UserCreationForm 并向其中添加一个字段,您最终会得到一个新的表单类,其中仅包含原始字段和您的新字段。

class MyForm(UserCreationForm):
    extra_field = forms.CharField()

覆盖管理表单

如果您尝试覆盖管理站点默认使用的 UserCreationForm,则必须为 User< 注册一个新的 ModelAdmin /代码> 现代。

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

from foo import MyNewUserCreationForm

class NewUserAdmin(UserAdmin):
    add_form = MyNewUserCreationForm

admin.site.unregister(User)
admin.site.register(User, NewUserAdmin)

Changing the default validator messages

You can change the error messages for the default validators via the error_messages argument to a form field.

To find out which validators exist per field, check here: https://docs.djangoproject.com/en/dev/ref/forms/fields/#built-in-field-classes

class MyForm(UserCreationForm):
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields['username'].error_messages = {'invalid': 'foobar'}
        self.fields['password1'].error_messages = {'required': 'required, man'}

Adding new fields to an existing form

If you want to add new fields, you'd add them via subclassing (which is just python).

If you subclass UserCreationForm and add a field to it, you end up with a new form class that simply has the original's fields and your new ones.

class MyForm(UserCreationForm):
    extra_field = forms.CharField()

Overriding the admin form

If you are trying to override the UserCreationForm that the admin site uses by default, you'll have to register a new ModelAdmin for the User moder.

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

from foo import MyNewUserCreationForm

class NewUserAdmin(UserAdmin):
    add_form = MyNewUserCreationForm

admin.site.unregister(User)
admin.site.register(User, NewUserAdmin)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文