I am familiar with creating a superuser in a Django App using python manage.py createsuperuser command in terminal. I am also familiar with creating a superuser using Python Shell.
But I want to know that how do we register a user as an admin in a Django App using RegistrationModel and views.py? Or more specifically, how to register the user and change it's status to staff or superuser in views.py?
STEP ONE
After creating and activating virtual environment Let's create our new project (authentication) and a new app (users) using the following command
Next is we add users app to installed_apps in authentication/settings.py file
STEP TWO
We create CustomUser model in our users/urls.py file
In other to alert django to use the CustomUser model for authentication, we add
AUTH_USER_MODEL = 'users.CustomUser'to our authentication/settings.py fileSTEP THREE
Create a new file forms.py in users app and add the following
STEP FOUR
Since we customized our
UserCreationFormandUserChangeForm, we have to update in our users/admin.py fileSTEP FOUR
We can proceed to making migrations and migrating the tables to database using the command
STEP FIVE
Create the template for creating new users. We create a new folder (
templates) in our parent dir where we can store ourhtmlfiles and create a new filecreate_new.htmlin thetemplatesfolderCommand for this:
Update the templates settings
Add the following to templates/create_new.html file
STEP SIX
Create view to display this template
STEP SEVEN
Link view to a specific url
STEP EIGHT
Run local server and create new user to login to the admin-site. *No need of
python manage.py createsuperusercommand