Hi @CodenameTim thank you for taking the time to attend to my issue and my apologies for attaching screenshots. Well what I’m actually trying to do is to make sure that no one access the custom admin registration page without entering the admin code. And so I have come up with a decorator to handle this but it’s not taking me to the admin register page as I anticipated, in fact when I entered the correct code as well, it’s still redirecting to the same admin validate page. I have identified this issue which is in my template file and have fixed it () in my admin_code.html template. However when I entered the correct code and successfully go to the admin registration page, the admin register form appears to be not working perfectly as it was before (without the decorator). All I could see on each fields on the admin register form is this error “This field is required” and when I clicked submit without filling any field, it just take me to back to the admin validate page without validating the fields. It should validate the fields and if all the information is valid, it will create an admin account and redirect to the login page. But it’s not doing that.
Here is the code in decorators.py:
“”"
from django.contrib.auth.decorators import user_passes_test
from django.urls import reverse_lazy
from django.conf import settings
#version 1
def admin_code_entered(function=None):
actual_decorator = user_passes_test(
lambda u: hasattr(u, ‘admin_code_entered’) and u.admin_code_entered == settings.ADMIN_CODE,
login_url=reverse_lazy(‘admin_validate’),
redirect_field_name=None
)
if function:
return actual_decorator(function)
return actual_decorator
“”"
Here is the code in views.py:
“”"
from django.contrib.auth import login, logout, authenticate
from django.shortcuts import redirect, render
from django.contrib import messages
from . forms import AdminCodeForm, AdminSignUpForm, CustomerSignUpForm, EmployeeSignUpForm
from django.contrib.auth.forms import AuthenticationForm
from django.conf import settings
from .decorators import admin_code_entered
#register general view
def register(request):
return render(request, ‘register.html’)
#version1-the admin registration validation view
def admin_validate(request):
form = AdminCodeForm(request.POST or None)
if form.is_valid():
if form.cleaned_data[‘admin_code’] == settings.ADMIN_CODE:
return redirect(‘admin_register’)
else:
form.add_error(‘admin_code’, ‘Incorrect admin code.’)
return render(request, ‘admin_code.html’, {‘form’: form})
#@admin_code_entered
def admin_register(request):
if request.method == ‘POST’:
form = AdminSignUpForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
return redirect(‘login’)
else:
form = AdminSignUpForm()
return render(request, ‘admin_register.html’, {‘form’: form})
“”"
Here’s the code in my forms.py:
“”"
from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.db import transaction
from .models import User, Admin, Customer, Employee
from django.conf import settings
#the user will have to enter the admin code inorder to register as admin
class AdminCodeForm(forms.Form):
admin_code = forms.CharField(max_length=50)
def clean_admin_code(self):
admin_code = self.cleaned_data.get(‘admin_code’)
if admin_code != settings.ADMIN_CODE:
raise forms.ValidationError(‘Invalid admin code’)
return admin_code
#admin registration form
class AdminSignUpForm(UserCreationForm):
email = forms.EmailField(required=True)
first_name = forms.CharField(required=True)
last_name = forms.CharField(required=True)
phone_number = forms.CharField(required=True)
position = forms.CharField(required=True)
class Meta(UserCreationForm.Meta):
model = User
#verify email
def clean_email(self):
email = self.cleaned_data.get('email')
if User.objects.filter(email=email).exists():
raise forms.ValidationError('This email address is already in use.')
return email
@transaction.atomic
def save(self):
user = super().save(commit=False)
user.email = self.cleaned_data.get('email')
user.is_superuser = True
user.is_staff = True
user.is_admin = True
user.first_name = self.cleaned_data.get('first_name')
user.last_name = self.cleaned_data.get('last_name')
user.save()
admin = Admin.objects.create(user=user)
admin.phone_number=self.cleaned_data.get('phone_number')
admin.position=self.cleaned_data.get('position')
admin.save()
return user
“”"
Please note that the admin code is hard coded in the project settings.
Here is the link to the project on GitHub if you’d like to do further investigation or customize GitHub - billwhite16/custom_user: This project has three users.
Thank you in advance for you help.