Accepting email address as username in Django

后端 未结 12 2062
感动是毒
感动是毒 2020-11-28 19:48

Is there a good way to do this in django without rolling my own authentication system? I want the username to be the user\'s email address instead of them creating a userna

12条回答
  •  孤独总比滥情好
    2020-11-28 20:29

    I think the most quickly way is to create a form inherit from UserCreateForm, and then override the username field with forms.EmailField. Then for every new registration user, they need to signon with their email address.

    For example:

    urls.py

    ...
    urlpatterns += url(r'^signon/$', SignonView.as_view(), name="signon")
    

    views.py

    from django.contrib.auth.models import User
    from django.contrib.auth.forms import UserCreationForm
    from django import forms
    
    class UserSignonForm(UserCreationForm):
        username = forms.EmailField()
    
    
    class SignonView(CreateView):
        template_name = "registration/signon.html"
        model = User
        form_class = UserSignonForm
    

    signon.html

    ...
    
    ... ...
    ...

提交回复
热议问题