while preparing registration form i cannot save/register a user. it is not saved in admin panel.it shows the above error.kindly help me.
VIEWS
from django.contrib import messages
from django.contrib.auth import get_user_model,logout
from django.contrib.auth.views import LoginView
from django.shortcuts import HttpResponseRedirect
from django.urls import reverse_lazy
from django.views.generic import TemplateView, RedirectView
from .forms import UserRegistrationForm
User = get_user_model()
class UserRegistrationView(TemplateView):
model = User
form_class = UserRegistrationForm
template_name = 'accounts/user_registration.html'
def dispatch(self, request, *args, **kwargs):
if self.request.user.is_authenticated:
return HttpResponseRedirect(
reverse_lazy('transactions:transaction_report')
)
return super().dispatch(request,*args, **kwargs)
def post(self, request, *args, **kwargs):
registration_form = UserRegistrationForm(self.request.POST)
if registration_form.is_valid():
user = registration_form.save()
user.save()
# login(self.request, user)
messages.success(
self.request,
(
f'Thank You For Creating A Bank Account. '
f'Your Account Number is {user.account.account_no}. '
)
)
return HttpResponseRedirect(
reverse_lazy('accounts:user_login')
)
return self.render_to_response(
self.get_context_data(
registration_form=registration_form,
)
)
def get_context_data(self, **kwargs):
if 'registration_form' not in kwargs:
kwargs['registration_form'] = UserRegistrationForm()
return super().get_context_data(**kwargs)
class UserLoginView(LoginView):
template_name='accounts/user_login.html'
redirect_authenticated_user = False
class LogoutView(RedirectView):
pattern_name = 'home'
def get_redirect_url(self, *args, **kwargs):
if self.request.user.is_authenticated:
logout(self.request)
return super().get_redirect_url(*args, **kwargs)
MODELS
from decimal import Decimal
from django.contrib.auth.models import AbstractUser
from django.core.validators import (
MinValueValidator,
MaxValueValidator,
)
from django.db import models
from .managers import UserManager
class User(AbstractUser):
username = None
email = models.EmailField(unique=True, null=False, blank=False)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
def __str__(self):
return self.email
@property
def balance(self):
if hasattr(self, 'account'):
return self.account.balance
return 0
class BankAccountType(models.Model):
name = models.CharField(max_length=128)
maximum_withdrawal_amount = models.DecimalField(
decimal_places=2,
max_digits=12
)
annual_interest_rate = models.DecimalField(
validators=[MinValueValidator(0), MaxValueValidator(100)],
decimal_places=2,
max_digits=5,
help_text='Interest rate from 0 - 100'
)
interest_calculation_per_year = models.PositiveSmallIntegerField(
validators=[MinValueValidator(1), MaxValueValidator(12)],
help_text='The number of times interest will be calculated per year'
)
def __str__(self):
return self.name
def calculate_interest(self, principal):
"""
Calculate interest for each account type.
This uses a basic interest calculation formula
"""
p = principal
r = self.annual_interest_rate
n = Decimal(self.interest_calculation_per_year)
# Basic Future Value formula to calculate interest
interest = (p * (1 + ((r/100) / n))) - p
return round(interest, 2)
class UserBankAccount(models.Model):
user = models.OneToOneField(
User,
related_name='account',
on_delete=models.CASCADE,
)
account_type = models.ForeignKey(
BankAccountType,
related_name='accounts',
on_delete=models.CASCADE
)
account_no = models.PositiveIntegerField(unique=True)
# gender = models.CharField(max_length=1, choices=GENDER_CHOICE)
# birth_date = models.DateField(null=True, blank=True)
balance = models.DecimalField(
default=0,
max_digits=12,
decimal_places=2
)
interest_start_date = models.DateField(
null=True, blank=True,
help_text=(
'The month number that interest calculation will start from'
)
)
initial_deposit_date = models.DateField(null=True, blank=True)
def __str__(self):
return str(self.account_no)
def get_interest_calculation_months(self):
"""
List of month numbers for which the interest will be calculated
returns [2, 4, 6, 8, 10, 12] for every 2 months interval
"""
interval = int(
12 / self.account_type.interest_calculation_per_year
)
start = self.interest_start_date.month
return [i for i in range(start, 13, interval)]
# class UserAddress(models.Model):
# user = models.OneToOneField(
# User,
# related_name='address',
# on_delete=models.CASCADE,
# )
# street_address = models.CharField(max_length=512)
# city = models.CharField(max_length=256)
# postal_code = models.PositiveIntegerField()
# country = models.CharField(max_length=256)
URLS
from django.urls import path
from .views import UserRegistrationView, LogoutView, UserLoginView
app_name = 'accounts'
urlpatterns = [
path(
"login/", UserLoginView.as_view(),
name="user_login"
),
path(
"logout/", LogoutView.as_view(),
name="user_logout"
),
path(
"register/", UserRegistrationView.as_view(),
name="user_registration"
),
]
FORMS
from django import forms
from django.conf import settings
from django.contrib.auth.forms import UserCreationForm
from django.db import transaction
from .models import User, UserBankAccount
class UserRegistrationForm(UserCreationForm):
first_name = forms.CharField(widget=forms.TextInput(attrs={'autofocus':'on'}))
email = forms.CharField(widget=forms.EmailInput(attrs={'autofocus':'off'}))
class Meta:
model = User
fields = [
'first_name',
'last_name',
'email',
'password1',
'password2',
]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field in self.fields:
self.fields[field].widget.attrs.update({
'class': (
'appearance-none block w-full bg-gray-200 '
'text-gray-700 border border-gray-200 '
'rounded py-3 px-4 leading-tight '
'focus:outline-none focus:bg-white '
'focus:border-gray-500'
)
})
@transaction.atomic
def save(self, commit=True):
user = super().save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
UserBankAccount.objects.create(
user=user,
account_no=(
user.id +
settings.ACCOUNT_NUMBER_START_FROM
)
)
return user
These are my codes.recently i removesome fields from registration form after that i can't save the user in admin panel.Registration form is not saved.kindly help me to find and correct my mistake