Do you want to see all the data submitted by the user on your Django website? That’s exactly you need an admin dashboard where you can see all the models and data entries.
Django has a special admin dashboard feature. In Django, models are defined to store the user data.
As part of the Django tutorial, we are going to see how we can add model to Django admin so that we can see all the model data entries from users.
If you have already created model, you can skip this step.
As an example, let’s take a model called ContactModel
which has three fields-
name
to store the name of the usermobile
to store the mobile number of the useremail
to store the email ID of the user to contactYou can define the model in the models.py
as below.
class ContactModel(models.Model):
name = models.CharField(max_length=120)
mobile = models.IntegerField()
email = models.EmailField()
In an earlier tutorial, we have learned about creating a form in Django. Similarly you can create the contact form to allow users to enter the data that will be saved in this model.
Being admin, we might want to see who all have contacted us by submitting the custom contact form.
Django has admin dashboard feature by which we can see or track all the data models or information.
Once user share the contact detail by filling the contact form, let’s see how we can check the information.
You can check the admin.py
file in your project app directory. If it is not there, just create one.
Edit admin.py
and add below lines of code to register model for admin dashboard.
from django.contrib import admin
from .models import ContactModel
@admin.register(ContactModel)
class RequestDemoAdmin(admin.ModelAdmin):
list_display = [field.name for field in
ContactModel._meta.get_fields()]
Here, we are showing all the model fields in the admin site. You can also customize the list_display
list by specifying selected fields.
For example, we want to display only a name and an email ID (and not a mobile number) in the Django admin site.
from django.contrib import admin
from .models import ContactModel
@admin.register(ContactModel)
class RequestDemoAdmin(admin.ModelAdmin):
list_display = ['name', 'email']
That’s all. Now run your Django project.
If you have created the new model, you have to migrate the models into the database. Execute the below command in the console using manage.py
script.
Make Migrations:
python manage.py makemigrations
Migrate:
python manage.py migrate
Now let’s start your Django project by running Django server.
python manage.py runserver
Once it is started, open the Django admin dashboard in the browser.
http://127.0.0.1:8000/admin
Once you login into your admin dashboard, you can see the contact model in your dashboard.
Click on the “Contact model”. You will see all the model data entries.
Note: Field ID
is autogenerated fields that uniquely define each entry in the model.
You can also customize the Django admin dashboard to make it more friendly tracking all the models data.
This is all about how you can add model to Django admin. If you find any difficulty, let me know in the comment section below.
Not showing data after submitting the registeration form.
Please share the console log or error. Without that, it is hard to trace the problem.
Good Morning
sir,
register form submitting properly but not in the admin without any error,
Just one question, how did you make the main column? I am having an issue in that.
You have to define the Django model and then add it to the admin dashboard.