0% found this document useful (0 votes)
13 views

Fullstack Development - Module 3

Uploaded by

swarajbharathp
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Fullstack Development - Module 3

Uploaded by

swarajbharathp
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

||Jai Sri Gurudev ||

Sri AdichunchanagiriShikshana Trust®

SJB INSTITUTE OF TECHNOLOGY


Accredited by NBA & NAAC with ‘A+’ Grade
No. 67, BGS Health & Education City, Dr. Vishnuvardhan Road Kengeri,
Bangalore – 560 060

Department of Computer Science & Engineering


FullStack Development [21CS62]
MODULE - 3
Django Admin Interfaces and Model Forms

6th SEMESTER – B. E
Academic Year: 2023 – 2024 (Even)
Fullstack
Development
Presenter: Mrs. Shilpashree S
Assistant Professor, Dept. of CSE,
SJB Institute of Technology
Django Admin Interfaces

The Django Administration Site:

One of Django’s killer features is its robust built-in admin interface that provides a visual
way to interact with data.

This is a Web-based interface, limited to trusted site administrators, that enables the
adding, editing and deletion of site content.

It reads metadata from your models to provide a quick, model-centric interface where
trusted users can manage content on your site.
6.1 Activating the Admin Interface

1. Add 'django.contrib.admin' and its dependencies -

django.contrib.auth

django.contrib.contenttypes

django.contrib.messages

django.contrib.sessions

to your INSTALLED_APPS setting.


2. Configure a Django Templates backend in your TEMPLATES setting with
django.template.context_processors.request,
django.contrib.auth.context_processors.auth,
django.contrib.messages.context_processors.messages in the 'context_processors' option of
OPTIONS.
If you’ve customized the MIDDLEWARE setting,
django.contrib.auth.middleware.AuthenticationMiddleware and
django.contrib.messages.middleware.MessageMiddleware must be included.
4. Create the models for providing a basic structure for managing your Django project.
You can extend them further by adding additional fields as needed.

5. Run migration commands

6. Register your models in admin.py:


from django.contrib import admin

# Register your models here.


from .models import Author, Publisher, Book
admin.site.register(Author)
admin.site.register(Publisher)
admin.site.register(Book)
7. Hook the admin’s URLs into your URLconf (urls.py):

from django.contrib import admin

from django.urls import path

urlpatterns = [

path('admin/', admin.site.urls),

]
8. Create a superuser account (a user who has all permissions). This is useful if you need to
create an initial superuser account or if you need to programmatically generate superuser
accounts for your site(s).
SuperUser Creation
python3 manage.py createsuperuser
Username: Provide Username
Email address: Provide Email Address
Password: Provide Password
Password (again): Type the password again
Superuser created successfully.

9. That’s it. Now run python manage.py runserver to start the development server.
The first thing you’ll see is a login screen. You’ll use the username and password you set up when you
added your superuser.
Each object registered in admin.py shows up on the main index page.
When you register a model in the Django administration site, you get a user-friendly
interface generated by introspecting your models that allows you to list, edit, create,
and delete objects in a simple way.
Admin Interface adds the data to the database with ease.
Django uses different form widgets for each type of field. Even complex fields, such as the DateTimeField,
are displayed with an easy interface, such as a JavaScript date picker.
Clicking the "+" button opens a popup form, allows to create a new publisher on the fly without pre-
populating any data.
Observe Recent Actions originated from “django_admin_log” (db.sqlite3)
Admin interface also handles input validation for you
When you delete an existing object, the admin interface asks you to confirm the delete action to avoid
costly mistakes.
Deletions also cascade; the deletion confirmation page shows you all the related objects that will be deleted as well.
6.2.1 Users, Groups, and Permissions

● Since we’re logged in as a superuser, we have access to create, edit, and delete any
object.
● However, the admin interface has a user permissions system that we can use to give
other users access only to the portions of the interface that they need.
● We can edit these users and permissions through the admin interface just like any
other object. The link to the User and Group models is there on the admin index
along with all the objects we’ve defined.
● User objects have the standard username, password, e-mail, and real name fields you
might expect, along with a set of fields that define what the user is allowed to do in
the admin interface.
6.2.1 Users, Groups, and Permissions - Contd..

First, there’s a set of three flags:


● The “is active” flag controls whether the user is active at all. If this flag is off, the user has no access to any
URLs that require login.
● The “is staff” flag controls whether the user is allowed to log in to the admin interface (i.e., whether that user
is considered a “staff member” in your organization). Since this same user system can be used to control
access to public (i.e., non-admin) sites (see Chapter 12), this flag differentiates between public users and
administrators.
● The “is superuser” flag gives the user full, unfettered access to every item in the admin interface; regular
permissions are ignored.

Each object editable through the admin interface has three permissions: a create permission, an edit permission,
and a delete permission. Assigning permissions to a user grants the user access to do what is described by those
permissions.
6.2.1 Users, Groups, and Permissions - Contd..

Note

Access to edit users and permissions is also controlled by this permission system. If you
give someone permission to edit users, she will be able to edit her own permissions, which
might not be what you want!

You can also assign users to groups. A group is simply a set of permissions to apply to all
members of that group. Groups are useful for granting identical permissions to large
number of users.
6.3 Customizing the Admin Interface

We can customize the way the admin interface looks and behaves in a number of ways. we can easily add some
display, searching, and filtering functions to this interface. Change the Admin declaration as follows:

class Book(models.Model):
title = models.CharField(maxlength=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date = models.DateField()
class Admin:
list_display = ('title', 'publisher', 'publication_date')
list_filter = ('publisher', 'publication_date')
ordering = ('-publication_date',)
search_fields = ('title',)
6.4 Customizing the Admin Interface Look and Feel

A Django administrative site is represented by an instance of


django.contrib.admin.sites.AdminSite; by default, an instance of this class is created as
django.contrib.admin.site and you can register your models and ModelAdmin instances
with it.

If you want to customize the default admin site, you can override it.
6.4 Customizing the Admin Interface’s Look and Feel - Contd..

Change the Site Header:

Include this in urls.py:

admin.site.site_header = “New Header”

Change the Site Title:

admin.site.site_title = “New Title”

For more options, explore the below link:


https://docs.djangoproject.com/en/5.0/ref/contrib/admin/#adminsite-objects
Topic of Self Study:

6.6 When and Why to Use the Admin Interface?

Come up with case studies on when and why admin interfaces are helpful.

You might also like