This example makes the users register and login
using the email instead of the default username
from django_registration.views import RegistrationView
from django_registration.backends.activation.views import *
class MyRegistrationView(RegistrationView):
def create_inactive_user(self, form):
"""
Create the inactive user account and send an email containing
activation instructions.
"""
new_user = form.save(commit=False)
new_user.is_active = False
new_user.username = new_user.email
new_user.save()
self.send_activation_email(new_user)
return new_user
from django.contrib.auth.models import User
from django_registration.forms import RegistrationFormUniqueEmail
class MyRegistrationForm(RegistrationFormUniqueEmail):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['password1'].help_text = None
self.fields['password2'].help_text = None
self.fields.pop(User.USERNAME_FIELD, None)
from django import forms
from django.contrib.auth.forms import AuthenticationForm
class MyAuthenticationForm(AuthenticationForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['username'].label = "Email"
self.fields['username'].widget = forms.EmailInput(attrs={'class': 'form-control'})
from django.contrib.auth import views as auth_views
from polls.views import *
from polls.forms import *
urlpatterns = [
...
path('accounts/register/', MyRegistrationView.as_view(form_class=MyRegistrationForm), name='registration_register'),
path('accounts/', include('django_registration.backends.activation.urls')),
path('accounts/login/', auth_views.LoginView.as_view(template_name="registration/login.html", form_class=MyAuthenticationForm, redirect_authenticated_user=True), name='login'),
path('accounts/', include('django.contrib.auth.urls')),
...
]
Keep in mind that the previously created super user username is not the email.
For this reason super users can only login through the admin interface.
Registration url:
http://localhost:8000/accounts/register/
Login url:
http://localhost:8000/accounts/login/
Python 3.12 Django==4.2 django-registration==3.3
27 June 2024
|
Last Updated: 22 Nov. 2025
|
jaimedcsilva Related