Django- Admin and Users registration Django by Ravinder Nath Rajotiya - January 9, 2022January 10, 20220 Share on Facebook Share Send email Mail Print Print Table of Contents Toggle IntroductionCreating super usermakemigrationsmigrate commandcreatesuperusersuperuser credentialUpdating the user registration FormCreate UserRegisterFormUpdate users views.pyFinal Touches Introduction Well, one of the important part of dynamic sites is to create users so that one can have access to the database to enter data, to delete , edit or update the data, to authorize users to have an access to certain data set. Django provide an admin page by default. This can be accessed just by typing admin after the url i.e. localhost:8000/admin and you will see a page as seen below: But you can’t do anything here right now. We have to first create super admin who will be responsible for the overall management and users with the roles to have access to certain pages or data. Let us first create a superuser from the command-line. Creating super user Well, If you have not yet created any database in your project then Django is going to throw some error like: So, Django provides a way of creating the superuser without first creating your application database using makemigrations and migrate commands. the Django command makemigrations applies changes to the database, but since we haven’t created our database, this migration will add some default table ‘auth_table’ for us in the migrations folder of the app. This will then allow us to use the command createsuperuser. Let us run these migrations commands makemigrations This command run successfully and show ”no changes detected’ , this is because we have not created any database or modified any. Makemigrations just detects changes and prepares Django to update database. In order to apply these migrations, we have to run migrate command migrate command so default database auth_user table is now updated and ‘auth_table’ should now exist. We can now use the command createsuperuser at the command-line. It will be working as shown in the following screenshot. createsuperuser The command is accepted by the Django and you will get prompts for filling user credential. this is shown below: superuser credential Fill all credentials as shown below: The superuser is now created. You can now go back, run the server and open the admin page. And login as admin. You will find the default admin page with groups and users tabs to operate with. If you had to do it from scratch, it would have taken lot of efforts and codes to develop this page. This how Django provides inbuilt features to relieve developers from development efforts. The users and groups tab allow you to create, add, delete, assign permissions / rights etc. You can work with this page and experience yourself the important admin tasks that can be executed from this admin page. Updating the user registration Form So the form is able to create the users. But if you see the above screenshot, the users email, first and last name was not updated. We don’t want to every time go to admin page and update the user information. Instead we wish to provide through the registration form. Create UserRegisterForm The {{form.as_p }} is printing the registration for as a whole and not just the individual fields for us. In order to add additional fields from the form itself. We have to create a new form ‘UserRegisterForm’ that inherits fields from UserCreationForm Creat a new file ‘forms.py’ inside the users app directory where we put this new form Here we create a new form ‘forms.py’ that inherits from UserCreationForm from django import forms from django.contrib.auth.models import User from django.contrib.auth.forms import UserCreationForm class UserRegisterForm(UserCreationForm): # inherit from UserCreationForm email=forms.EmailField() #default is True, so we left it blank class Meta: #gives nested namespace for configurations and keeps the configuration at one place model = User #whenever this form is going to validate, it is going to create a user # and the model it is going to affect is the User model, and when you save the #form it is going to save to this user model and the fields are as per the fields list fields = [‘username’, ’email’, ‘password1’, ‘password2’] # we can use this form in our user views. Go back and update the user # views.py by importing this’ UserRegisterForm’ from forms.py Update users views.py Remove – from django.contrib.auth.forms import UserCreationForm Import UserRegisterForm as : from .forms import UserRegisterForm Change username=UserCreationForm() to UserRegisterForm() at both places in views.py Save, and reload register page. You can now see the updated registration form with email field. Fill all credentials and click Sign Up. Go to admin page ‘localhost:8000/admin’ and click the user tab and you can see that all fields for the users were entered and saved. Final Touches Again when you look at the registration page there are lot of validations visible. This doesn’t give a good look. So what we can do is let the user fill all details, and if invalid password entry then the validation help can become visible. Let us see this final updates to the registration pages. We use a third-party Django application called CRISPY FORMS which allows us to add simple tags in our form that will style our form in a bootstrap style Step-1: install crispy forms application. Stop the server and use PIP command as: Here you have to ensure that you install the django-crispy-forms inside your venv environment directory otherwise when you load the page, it will show error Step-2: tell django that crispy_forms app is installed. Do this in settings.py: Step-3: Also tell crispy-forms which CSS framework we want to use. It defaults to bootstrap2, but bootstrap2 is quite old, so use bootstrap4. For any other version go to CRIPY documentation online to see how it can be changed for bootstrap5, Update in settings.py: CRISPY_TEMPLATE_PACK=’bootstrap4′ Step-4: Update this in register.html for as: at the top add- {% load crispy_forms_tags %} # will allow any crispy filters on our form Change the form as {{ form|crispy }} #here we use crispy filter Step-5: That is all. run the server, load the ‘register.html’ page and see, how nice the form now looks: Share on Facebook Share Send email Mail Print Print