NOT NULL constraint failed: auth_user.password

Viewed 466

Django inbuild authentication using time I'm faced this error ! ! NOT NULL constraint failed: auth_user.password

Views.py

from django.shortcuts import render
from django.views.generic import TemplateView, FormView
from .forms import UserRegistrationForm
from django.contrib.auth.models import User

class RegisterView(FormView):
    template_name = "registration/register.html"
    form_class = UserRegistrationForm
    success_url = '/'

    def form_valid(slef, form):
        username = form.cleaned_data.get('username')
        email = form.cleaned_data.get('email')
        password = form.cleaned_data.get('password')
        user = User.objects.create(username=username, email=email, password=password)
        user.save()
        return super().form_valid(form)

Forms.py

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

class UserRegistrationForm(UserCreationForm, forms.ModelForm):
    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')```
1 Answers

There no password field in the form.

class RegisterView(FormView):
    template_name = "registration/register.html"
    form_class = UserRegistrationForm
    success_url = '/'

    def form_valid(slef, form):
        username = form.cleaned_data.get('username')
        email = form.cleaned_data.get('email')
        password = form.cleaned_data.get('password1')  # password --> password1
        user = User.objects.create(username=username, email=email, password=password)
        user.save()
        return super().form_valid(form)

Please refer this https://docs.djangoproject.com/en/3.1/topics/auth/

Related