In this blog, let’s see how to render a model in django admin.The admin is enabled in the default project template used by startproject. For example, let us consider project- admin_model to demonstrate this. We start the Django project by typing this in the terminal.
django-admin startproject admin_model
Let’s go inside the newly created admin_model directory. Typing the following:
Now, let’s create our app model_admin. We do that by this command.
>python manage.py startapp model_admin
Include this app in setting file. Scroll down and make the following changes in settings.py
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'model_admin', ]
Now under model_admin , go to models.py to make an example Person model. In order to do so, make the following changes.
from django.db import models class Person(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) email = models.EmailField(max_length=254) phone = models.CharField(max_length=20) bio = models.TextField(max_length=500)
Now, go to admin.py to include the model in the admin site.
Import this model and make the following changes.
from django.contrib import admin from .models import Person # Register your models here. admin.site.register(Person)
Before starting to use admin interface of Django one needs to create superuser in Django. In other words, a superuser is like an admin who can access and modify everything of a particular Django project. In order to create a superuser, enter the following command into the terminal.
>python manage.py createsuperuser
Enter your Name, Email, Password and confirm password.
To sum up, type python manage.py runserver in the terminal.
In conclusion, lets check our admin interface. visit http://localhost:8000/admin/
In order to add data into the model, tap add and enter corresponding data into the required fields. click on save.
Model Person has been successfully rendered into the admin interface! This is how we render a model in admin interface. Similarly, multiple renders can be modelled and rendered in the admin interface.