Django Registration Redux: how to change the unique identifier from username to email and use email as login

后端 未结 3 1984
故里飘歌
故里飘歌 2020-12-19 20:21

I\'m using django-registration-redux in my project for user registration. It uses default User model which use username as the unique

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-19 21:20

    You can override registration form like this

    from registration.forms import RegistrationForm
    class MyRegForm(RegistrationForm):
        username = forms.CharField(max_length=254, required=False, widget=forms.HiddenInput())
    
        def clean_email(self):
            email = self.cleaned_data['email']
            self.cleaned_data['username'] = email
            return email
    

    And then add this to settings file (read this link for details)

    REGISTRATION_FORM = 'app.forms.MyRegForm'
    

    This will set the email to username field as well and then everything will work as email is now the username.

    The only problem is that username field has a max lenght of 30 in DB. So emails longer than 30 chars will raise DB exception. To solve that override the user model (read this for details).

提交回复
热议问题