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

Django Template System

Django Template System Django allows Python and HTML to be divided, the Python goes into views, and HTML goes into templates. Django relies on the render feature and the language of the Django Model...

5 minutes read.

Django Tutorial

Django Introduction It is a web application framework written in a python programming language. It based on the MVT( Model View Templet) design pattern. It takes a short time to build an application...

2 minutes read.

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 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 Framework

Django is a popular high-level open-source web development Python framework based on follows Model View Template (MVT) architecture that allows rapid development. It. It provides common functionality like Admin Interface,...

3 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.

How to connect Django with Mysql

Any web application contains some data that comes from the input of the end-users. Thus, the database is required to handle those data. Several databases could be used to connect...

3 minutes read.

How to Implement Multiple User Types with Django

Django Framework: Django is a framework that can make web applications more accessible and faster. Django is an open-source collection of python libraries, and Django can be used for both the...

3 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 REST Framework

Django REST Framework (DRF)is an open-source, powerful, and flexible toolkit used to build RESTful web APIs. It is built upon the Django framework. DRF increases the development speed. Django and...

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.

How to Uninstall Django from cmd

Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Django Project

Django Project Let's start to use it. Each web app you want to build in Django is called a project; and a project is a collection of apps. A software is a series of...

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.

Creating View in Django

Creating View in Django    Django views are a key component of the frameworkbased applications.We are using Python function or class at their simplest, which takes up a web query and returns a web response.Views are used to get objects from the server, change objects if needed, render types, return HTML, and much more. Class Based View and Function based Django has two types of views: views based on functions (FBVs) and views based on class (CBVs).Initially, Django began with only FBVs, but then introduced CBVs as a way to model features so we didn't have to write boilerplate (i.e. the same software) code again and again.CBVs would be a way to increase efficiency, so you don't have to write as much code as you can. CBVs are classes of Python at their heart. Django ships with a range of "model" CBVs with pre-configured features that can be reused and often expanded.Then helpful names are given to these classes that explain what kind of functionality they provide.These are often referred to as "universal views" because they provide solutions to specific needs. The classes have a journal View function, or "view" for short, is simply a Python program that uses a web query and returns a web response. This response may include the HTML content of a web page, or a redirect, or a 404 error, or an XML file, or an image, etc. Example: You use a view to create a web page, remember that you need to connect a view to a URL to see it as a web page. Simple View We will create a simple view in myapplication to say “Welocome to django application” See the following view – from django.http import HttpResponse ...

4 minutes read.

Django CSRF

Django is a high-level python based web framework. Django is open source and free to use for all. Django provides many features to web developers like scalability, security, rapid development,...

4 minutes read.

Django DRF

Django Rest Framework, which is also referred to as DRF, is a very powerful application to build RESTful APIs in a Django application. DRF uses the Django framework. Similar to...

3 minutes read.

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...

3 minutes read.