1

As we know that the default Django login page form fields are the username and the password, however, I wanted to change the login fields to Email and Password, as I think that it's easy for people to login with their email than their username. And Also because the web app I'm trying to create doesn't need a username field. So, How Can I Change the fields to Email And Password?

I used this code to change the fields in the Forms.py:

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm


class UserRegistrationForm(UserCreationForm):
    email = forms.EmailField()

    class Meta:
        model = User
        fields = ['email', 'password1', 'password2']

Views.py:

from django.shortcuts import render, redirect
from .forms import UserRegistrationForm


def register(request):
    if request.method == 'POST':
        form = UserRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('login')
    else:
        form = UserRegistrationForm()
    return render(request, 'users/register.html', {'form': form})

Any Help Would Be Appreciated!

Heisenberg
  • 475
  • 8
  • 24

0 Answers0