Lesson 27 of 60 – Django Admin Panel
45%

Django Admin Panel

The Django Admin Panel is a built-in interface that allows developers and administrators to manage application data through a web browser. It can be used to add, view, edit, and delete database records without creating a separate management interface from scratch.

Note: Django's admin site is included with a standard Django project and can be customized by registering and configuring your models.

1. What is Django Admin?

Django Admin is a web-based management interface provided by Django.

It allows authorized users to manage registered models through a browser. For example, a school application can use the admin panel to manage students, courses, and fees.

2. Why Use Django Admin?

Django Admin can save development time because it provides many common database management features automatically.

  • Add records
  • View records
  • Edit records
  • Delete records
  • Search records
  • Filter records
  • Manage users and permissions

3. Admin URL

A standard Django project usually includes an admin URL in the main urls.py.

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

After starting the development server, the admin panel can normally be accessed at:

http://127.0.0.1:8000/admin/

4. Admin URL Configuration

The project's urls.py normally contains:

from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]

This connects the /admin/ URL to Django's admin site.

5. Admin Application

The Django admin application is normally included in INSTALLED_APPS.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

These built-in applications provide features used by Django's admin and authentication systems.

6. Database Migrations for Admin

Before using the admin site, apply the project's migrations.

python manage.py migrate

This creates the database tables required by Django's built-in applications, including authentication and admin-related data.

7. Create a Superuser

A superuser can be created using Django's createsuperuser command.

python manage.py createsuperuser

Django will ask for information such as username, email address, and password.

8. Superuser Username

During superuser creation, Django asks you to provide a username.

Username: admin

You can choose an appropriate username according to your project.

9. Superuser Email

Django may also ask for an email address when creating the superuser.

Email address: admin@example.com

The exact prompts can depend on the user model and Django configuration.

10. Superuser Password

Django asks you to enter and confirm a password.

Password:
Password (again):

Choose a strong password and keep it secure.

11. Run the Development Server

Start the Django development server using:

python manage.py runserver

Then open the admin URL in a browser:

http://127.0.0.1:8000/admin/

12. Admin Login Page

The admin URL displays a login page where an authorized user can enter their username and password.

After successful authentication, Django displays the admin interface according to the user's permissions.

13. Registering a Model

Creating a model does not automatically make it visible in the admin interface. The model can be registered in the app's admin.py.

from django.contrib import admin
from .models import Student

admin.site.register(Student)

14. Example Student Model

from django.db import models

class Student(models.Model):

    name = models.CharField(
        max_length=100
    )

    email = models.EmailField()

    age = models.IntegerField()

    def __str__(self):
        return self.name

This model can be registered in the admin panel.

15. Register Student Model

In students/admin.py:

from django.contrib import admin
from .models import Student

admin.site.register(Student)

After registration, the Student model can appear in the admin interface.

16. Add Records Through Admin

After registering the Student model, an authorized administrator can use the admin interface to add student records.

For example:

  • Name
  • Email
  • Age

Django generates the corresponding form based on the model fields.

17. Edit Records

Existing records can be opened from the admin list and edited through the generated form.

After changing the values, save the object to update the database.

18. Delete Records

The admin interface can also provide options to delete records.

Warning: Deleting database records can be destructive. Check the record and its related data before confirming a deletion.

19. ModelAdmin

Django provides ModelAdmin for customizing how a model is displayed and managed in the admin interface.

from django.contrib import admin
from .models import Student

class StudentAdmin(admin.ModelAdmin):
    pass

admin.site.register(Student, StudentAdmin)

20. list_display

The list_display option controls which model fields are shown in the admin list page.

class StudentAdmin(admin.ModelAdmin):

    list_display = (
        'name',
        'email',
        'age',
    )

admin.site.register(
    Student,
    StudentAdmin
)

21. search_fields

The search_fields option adds searchable fields to the admin list page.

class StudentAdmin(admin.ModelAdmin):

    search_fields = (
        'name',
        'email',
    )

Administrators can then search records using these fields.

22. list_filter

The list_filter option provides filtering options in the admin list page.

class StudentAdmin(admin.ModelAdmin):

    list_filter = (
        'is_active',
    )

It is useful when administrators need to narrow down a list of records.

23. Ordering Records

The ordering option can specify the default ordering of records in the admin list.

class StudentAdmin(admin.ModelAdmin):

    ordering = (
        'name',
    )

Use - before a field name for descending order.

ordering = (
    '-id',
)

24. readonly_fields

The readonly_fields option can make selected fields read-only in the admin form.

class StudentAdmin(admin.ModelAdmin):

    readonly_fields = (
        'created_at',
    )

This can be useful for values that administrators should see but not manually change.

25. Admin Fieldsets

The fieldsets option can organize fields into sections on the admin form.

class StudentAdmin(admin.ModelAdmin):

    fieldsets = (
        ('Student Information', {
            'fields': (
                'name',
                'email',
            )
        }),
        ('Other Information', {
            'fields': (
                'age',
            )
        }),
    )

26. Admin Permissions

Django's authentication and authorization system supports permissions for admin users.

Users can have different levels of access depending on their permissions, groups, and user status.

  • View permission
  • Add permission
  • Change permission
  • Delete permission

27. Admin Groups

Django groups allow permissions to be assigned to multiple users.

For example, a project could create groups such as:

  • Editors
  • Managers
  • Staff

The exact permissions can be configured according to the application's needs.

28. Customizing the Admin Site

The Django admin can be customized in many ways, including:

  • List columns
  • Search fields
  • Filters
  • Ordering
  • Read-only fields
  • Form fieldsets
  • Model permissions
  • Admin site titles and headers

This allows the built-in admin interface to fit the needs of a project.

29. Complete Admin Example

models.py

from django.db import models

class Student(models.Model):

    name = models.CharField(
        max_length=100
    )

    email = models.EmailField()

    age = models.IntegerField()

    is_active = models.BooleanField(
        default=True
    )

    def __str__(self):
        return self.name

admin.py

from django.contrib import admin
from .models import Student

class StudentAdmin(admin.ModelAdmin):

    list_display = (
        'name',
        'email',
        'age',
        'is_active',
    )

    search_fields = (
        'name',
        'email',
    )

    list_filter = (
        'is_active',
    )

admin.site.register(
    Student,
    StudentAdmin
)

Run migrations and start the server:

python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver

Then open the admin page and log in with an authorized account.

30. Summary of Django Admin

The Django Admin Panel provides a ready-made interface for managing registered Django models.

  • Django provides a built-in admin interface.
  • The admin URL is commonly /admin/.
  • Use createsuperuser to create an administrative account.
  • Models can be registered in admin.py.
  • ModelAdmin allows admin customization.
  • list_display controls list columns.
  • search_fields adds search functionality.
  • list_filter adds filtering options.
  • ordering controls default record ordering.
  • Django permissions control what users can do.

📌 Key Points

  • Django Admin is a built-in web interface for managing application data.
  • Use python manage.py createsuperuser to create a superuser.
  • Register models in admin.py.
  • ModelAdmin can customize how models appear in the admin.
  • list_display controls columns shown on the list page.
  • search_fields provides search functionality.
  • list_filter provides filtering functionality.
  • Permissions and groups control access to admin functionality.
  • The admin interface can manage database records without building every CRUD screen manually.

🧠 Quick Quiz

Question: Which command is used to create a Django superuser?