如何使django注册使用我自定义的UserCreationForm和UserChangeForm? [英] How to make django-registration use my customized UserCreationForm and UserChangeForm?

查看:1129
本文介绍了如何使django注册使用我自定义的UserCreationForm和UserChangeForm?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django 1.4和 django-registration 开发一个网站



我想允许用户使用任意Unicode字符创建他们的用户名。目前,如果有人尝试使用非拉丁字符进行注册,他将看到一条错误消息。负责拒绝此类非ASCII用户名的代码在 UserCreationForm UserChangeForm 中,请参阅 here

  username = forms.RegexField(
label = _(Username),max_length = 30,regex = r^ [\w。@ + - ] $,
help_text = _(必填,30个字符或更少字母,数字和
@ /。/ + / - / _ only。),
error_messages =
'invalid':_(此值可能只包含字母,数字和
@ /。/ + / - / _字符)})

现在,我想更改它。我看到一些建议,我应该创建我自己的 UserCreationForm ,并创建一个派生类有一些改变的行为似乎很容易。



我不明白的是:如何使 django-registration 使用我自定义的 UserCreationForm UserChangeForm ?显然,如果可能,我想避免修改 django-registration 的来源。

解决方案

请参阅 http://docs.b- list.org/django-registration/0.8/views.html



创建自己的注册表后,您可以将此表单传递给django-注册。查找 registration.backends.default.urls 模块

  url r'^ register / $',注册,
{'后端':'registration.backends.default.DefaultBackend',
'form_class':MyRegistrationForm},


I am developing a website using Django 1.4 and django-registration

I would like to allow users to create their user names using arbitrary Unicode characters. Currently, if someone tries to register using non-latin characters, he will see an error message. The code responsible for rejecting this kind of non-ASCII usernames is in UserCreationForm and UserChangeForm, see here:

username = forms.RegexField(
        label=_("Username"), max_length=30, regex=r"^[\w.@+-]+$",
        help_text = _("Required. 30 characters or fewer. Letters, digits and "
                      "@/./+/-/_ only."),
        error_messages = {
            'invalid': _("This value may contain only letters, numbers and "
                         "@/./+/-/_ characters.")})

Now, I would like to change it. I've seen some suggestions that I should create my own UserCreationForm, and creating a derived class with some changed behaviour seems easy enough.

The bit I don't understand is: how to make django-registration use my customized UserCreationForm and UserChangeForm? Obviously, I would like to avoid modifying the source of django-registration, if at all possible.

解决方案

See http://docs.b-list.org/django-registration/0.8/views.html

After creating your own registration form you can pass this form to the register view of django-registration. Look for the registration.backends.default.urls module

url(r'^register/$', register,
     {'backend': 'registration.backends.default.DefaultBackend', 
     'form_class': MyRegistrationForm},

这篇关于如何使django注册使用我自定义的UserCreationForm和UserChangeForm?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆