Django Admin

To manage the content of the website, e.g., adding and deleting posts, web developers need an admin portal on the website. The admin portal allows trusted users, also knows as administrators, to edit, create and manage the content on the website. Django comes with a built-in user interface for the administration portal. The UI of the portal is automatically generated by Django based on the project. The portal communicates with models to provide an interface where the admin can manage content. The built-in feature provides developers with some functionalities like user authentication, display forms, or validation of input. We can also add many features to customize the portal.

Setting up Admin Portal

For the admin portal to work, we need some modules in the INSTALLED_APPS and MIDDLEWARES_CLASSES of the firstproject/settings.py file.

These modules should be in INSTALLED_APPS:

INSTALLED_APPS = (
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'myapp',
)

And for MIDDLEWARE_CLASSES:

MIDDLEWARE_CLASSES = (
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.common.CommonMiddleware',
   'django.middleware.csrf.CsrfViewMiddleware',
   'django.contrib.auth.middleware.AuthenticationMiddleware',
   'django.contrib.messages.middleware.MessageMiddleware',
   'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

Django automatically creates the admin site for us. All we need to do is to create a superuser that has access to the admin portal. To access the admin site, run this command in the console to initiate the database.

Python manage.py migrate

This will create all the tables related to the data type in the database.

Now to create the superuser, use the following code-

python manage.py createsuperuser

Now enter the details of the superuser to access the portal.

Username: admin

Email address: admin@gmail.com

Password: *****

Re-enter password: *****

Now just run the development server by using the following command:

python manage.py run server.

After making sure the development server is running, go to the URL http://127.0.0.1:8000/admin/ where you will see the admin portal.

Django Admin

We can log in to the portal by entering the credentials created above.

After logging into the portal, you will be redirected to the admin dashboard page.

Django Admin

The Django framework has divided the Authentication and Authorization into two content named Groups and Users.

Register Models

Now the next step is to register the models in our app with the admin. Model registration is done in the admin.py file, and to register the model, first import the admin module and use admin.site.register(model_name)to register a model.

From Django. contrib import admin.


from .models import Author, Genre, Book, BookInstance


admin.site.register(Venue)
admin.site.register(Event)

This is one of the most basic ways of registering models. Django admin provides some customization features as well to improve the functionality and interface of the portal.

Django Admin

Since we have registered the models with admin, now we can edit, delete or add the Venues or events from the admin interface. These operations will also reflect in the mapped database table. If we click on one of the models here, it will open the list where we can do CRUD operation on the model's record.

Django Admin

To edit an already existing record, click on the record. If you want to create a new record, click on the add button.

Now if we want to leave some field blank in any model it will cause some error. Sometimes we would like to create some optional field which may remain blank if user wants.  To make some fields optional, use this code in defining the model.

class Venue(models.Model):
name = models.CharField('Venue Name', max_length=120)
address = models.CharField(max_length=300)
zip_code = models.CharField('Zip/Post Code', max_length=12)
phone = models.CharField('Contact Phone', max_length=20, blank=True)
web = models.URLField('Web Address', blank=True)
email_address = models.EmailField('Email Address', blank=True)
  
 def __str__(self):
 return self.name

As you can see, in the phone, web, and email_address blank option is set to True, which will make these fields optional. Now since we have changed the model, and we need to update the database as well by running commands:

python manage.py makemigrations
python manage.py migrate

Now restart the development server and log in to the development server using the above-created credentials. Now add one record to the Venue model, and we can see the optional fields are not bold, which means we can keep them blank if we want.

Django Admin

Related Topics

Life Cycle of Django

Life Cycle of Django Request - Response life cycle in Django  Thebasic principle of http protocol is the client sends a request to the server based on the request data, and the server sends...

3 minutes read.

Django Filters

Django is a high-level python based web framework. Django is mostly used as a backend engine for websites. Nowadays, every website communicates to a database to fetch all the information...

3 minutes read.

Django Create Project

Django is an open-source free to use web framework which is based on Python. Django uses Model-View-Template as its architecture. The final version of Django was released in 2008. Django...

4 minutes read.

Django CMS

Django is one of the most popular open-source Python web development framework. Developers use Django to build a website from scratch. Like WordPress, Django CMS is also a Content Management...

9 minutes read.

Django Installation

Django Installation Django development environment consists of installing and setting up Python, Django, and a Database System since Django deals with a web application. Below the statement will guide you through installing Python 3.8.0 and...

2 minutes read.

Django and React

This article will discuss the overview of Django and React, their advantages, applications, and most importantly, how to implement react in Django. What is Django? Django is a free and open-source web...

6 minutes read.

Django – Sending E-mails

Django – Sending E-mails Django comes with a light engine that is ready and simple to use to send emails. Similar to Python, you just need to import a smtplib. You just need to import django.core.mail in Django. Edit the project settings.py file to start sending emails and set  all the following options: EMAIL_HOST – server with smtp.EMAIL_HOST_USER – smtp server login key.EMAIL_HOST_PASSWORD ? Password credential for the smtp server.EMAIL_PORT – server port smtp.EMAIL_USE_TLS or _SSL – if it is secure connection then set True. Simple E-mail Sending To submit a simple e-mail, let's create it a "simpleEmail" view. from django.core.mail import...

3 minutes read.

Django Channels

Channels is a project that uses Django and adds some functionalities beyond the traditional HTTP by using WSGI and ASGI.  We can use both synchronous and asynchronous requests with ASGI...

5 minutes read.

Django MVT

 MVT(Model View Template) MVT means Model View Template is a software design pattern The View is used to implement the business logic and to communicate with a model for data transmission and to...

2 minutes read.

Django Model Fields

In Django, if we want to store the information about anything, we use Models. It contains various fields about the data. Generally, each model represents a database where all the...

4 minutes read.

Django CMS vs WordPress

Both Django CMS and WordPress belong "Self-Hosted Blogging" or "Content Management System (CMS)" category, that is used to manage digital content. Bloggers usually use these platforms. Django CMS Django is one of...

4 minutes read.

Django Stack

Django is a popular Python web application framework that adheres to the "batteries-included" tenet. Batteries-guiding included idea is that common functionality for creating web applications should be provided with the...

5 minutes read.

Django Authentication

Django is an open-source free to use web framework which is based on Python. Django uses Model-View-Template as its architecture. The final version of Django was released in 2008. Django...

4 minutes read.

Django ORM

Django is a Python web framework that enables easy and quick web development. Django has a handy and powerful feature called Django Object Relational Mapper, also known as Django ORM,...

3 minutes read.

Django Environment Setup

Django is a free and open-source web framework, which uses Python as its programming language. It is based on Model View Template (MVT) architecture. A framework is a collection of...

4 minutes read.

Django Celery

Django is a python-based web framework. It is an open-source and free-to-use framework. Django uses the Model View Template architecture. Django provides many functionalities to web developers. One of these...

3 minutes read.

Best Django courses

In this tutorial, we will see various courses in python Django available on the internet. Django has become popular these days, so courses on it are also increasing day by...

7 minutes read.

Django All Auth

For any web application, user registration and authentication are the essential parts. In Django, there are several applications like django-registration-redux to perform these tasks, but the major drawback is that...

6 minutes read.

Django Logging

Every application developed has some bugs and errors. It is the responsibility of the developers to maintain the application by making it bug-free and error-free. To debug the application code...

5 minutes read.

Django Class Based Views

Class-based views were developed to solve the problem of customizing function-based generic views. We can arrange our view code Object-Oriented by using class views. Class-based views do more than function-based...

3 minutes read.