使用 django-registration 在 Django 1.2 中禁用电子邮件样式的用户名

发布于 2024-09-04 04:08:07 字数 276 浏览 5 评论 0原文

Django 1.2 允许用户名采用电子邮件地址的形式。

Django 1.2 中的更改:用户名可能 现在包含@、+、.和 - 字符

我知道这是一个非常需要的功能,但是如果您不想要新的行为怎么办?它会在配置文件 URL 中产生混乱的用户名,并且似乎会破坏 django 注册(如果用户使用电子邮件样式的用户名注册帐户,则 django 注册激活电子邮件中的链接将返回 404)。

有谁有恢复旧行为并禁用电子邮件式用户名的方法吗?

Django 1.2 allows usernames to take the form of an email address.

Changed in Django 1.2: Usernames may
now contain @, +, . and - characters

I know that's a much-requested feature, but what if you don't want the new behavior? It makes for messy usernames in profile URLs and seems to break django-registration (if a user registers an account with an email-style username, the link in the django-registration activation email returns 404).

Does anyone have a recipe for restoring the old behavior and disabling email-style usernames?

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

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

发布评论

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

评论(2

南风起 2024-09-11 04:08:07

没有直接的方法可以恢复到旧的行为。

处理此问题的最简单方法是根据您的需求强制对用户名进行客户端和服务器端验证。 django-registration 不是一个积极开发的组件,我不会指望来自该方向的任何内容。只需在您这边添加一些额外的验证即可。

引用 Jacob 关于此事

[...]另一个常见的请求是允许
使用电子邮件地址作为用户名。

自定义注册/注册表单可以
处理进一步的限制。

There is no straightforward way to revert back to old behavior.

The easiest way to handle this would be to enforce client and server side validation of usernames according to your demands. django-registration is not an actively developed component, I wouldn't count on anything coming from that direction. Just add some extra validation on your side.

To quote Jacob on this matter:

[...] another common request is to allow the
use of email addresses as usernames.

Custom registration/signup forms can
deal with further restrictions.

樱娆 2024-09-11 04:08:07

django-registration 实际上不是这里的问题。问题是我已经对其 RegistrationForm 进行了子类化,使用新的 help_text 重新定义了用户名字段。通过这样做,我阻止了它使用自己的正则表达式字段。为了解决这个问题,我必须将一些零散的内容从 RegistrationForm 中提取到我的EnhancedRegistrationForm 子类中。

请注意正则表达式行,它反映了旧式用户名字符限制(这就是我想要的)。

from registration.forms import RegistrationForm   

# Carry these over from RegistrationForm - needed in the form definition below
attrs_dict = {'class': 'required'}
from django.utils.translation import ugettext_lazy as _

class EnhancedRegistrationForm(RegistrationForm):
    first_name = forms.CharField(label='first name', max_length=30, required=True)
    last_name = forms.CharField(label='last name', max_length=30, required=True)    
    username = forms.RegexField(regex=r'^\w+
,
        max_length=30,
        widget=forms.TextInput(attrs=attrs_dict),
        help_text='Email addresses cannot be used as usernames.', 
        required=True,
        label=_("Username"),
        error_messages={'invalid':"You cannot use an email address as a username, sorry."})    

    class Meta:
        fields = ('first_name','last_name','username','email','password1','password2')  


    def save(self, *args, **kwargs):
        """
        Overriding save, so call the parent form save and return the new_user
        object.
        """
        new_user = super(EnhancedRegistrationForm, self).save(*args, **kwargs) 
        new_user.first_name = self.cleaned_data['first_name']
        new_user.last_name = self.cleaned_data['last_name']
        new_user.save()
        return new_user   

django-registration actually wasn't the problem here. The problem was that I had subclassed its RegistrationForm, redefining the username field with new help_text. In so doing, I had prevented it from using its own regex field. To fix this, I had to pull a few bits and pieces from RegistrationForm into my EnhancedRegistrationForm subclass.

Note the regex line, which mirrors the old-style username character restrictions (which is what I want).

from registration.forms import RegistrationForm   

# Carry these over from RegistrationForm - needed in the form definition below
attrs_dict = {'class': 'required'}
from django.utils.translation import ugettext_lazy as _

class EnhancedRegistrationForm(RegistrationForm):
    first_name = forms.CharField(label='first name', max_length=30, required=True)
    last_name = forms.CharField(label='last name', max_length=30, required=True)    
    username = forms.RegexField(regex=r'^\w+
,
        max_length=30,
        widget=forms.TextInput(attrs=attrs_dict),
        help_text='Email addresses cannot be used as usernames.', 
        required=True,
        label=_("Username"),
        error_messages={'invalid':"You cannot use an email address as a username, sorry."})    

    class Meta:
        fields = ('first_name','last_name','username','email','password1','password2')  


    def save(self, *args, **kwargs):
        """
        Overriding save, so call the parent form save and return the new_user
        object.
        """
        new_user = super(EnhancedRegistrationForm, self).save(*args, **kwargs) 
        new_user.first_name = self.cleaned_data['first_name']
        new_user.last_name = self.cleaned_data['last_name']
        new_user.save()
        return new_user   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文