Django / Django Admin Panel

Building a Custom Admin Interface

This tutorial focuses on building a custom admin interface in Django. We'll start with the basic setup and move on to advanced customization options, providing you with the tools …

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Explores Django Admin features and customization options.

Building a Custom Admin Interface

1. Introduction

The goal of this tutorial is to guide you through the process of creating a custom admin interface in Django. By the end of this tutorial, you'll have a firm understanding of how to customize Django's built-in admin interface to fit your project needs.

What you will learn:
1. Using and customizing Django's built-in admin interface
2. Creating custom admin actions
3. Customizing the admin list page
4. Creating custom admin forms

Prerequisites:

  • Basic knowledge of Python
  • Basic understanding of Django's MVT architecture
  • Django installed on your machine

2. Step-by-Step Guide

Django's admin interface is a powerful feature that can be customized to a great extent. This guide will walk you through the process step by step.

Setting Up Django Admin

Firstly, ensure that 'django.contrib.admin' is included in your INSTALLED_APPS setting:

INSTALLED_APPS = [
    #...
    'django.contrib.admin',
    #...
]

Registering Your Models

Register your models with the admin interface. In your app, create an admin.py file (if it doesn't exist) and register your model:

from django.contrib import admin
from .models import YourModel

admin.site.register(YourModel)

Customizing Admin Interface

You can customize the admin interface by creating an admin class that inherits from admin.ModelAdmin:

class YourModelAdmin(admin.ModelAdmin):
    pass

admin.site.register(YourModel, YourModelAdmin)

3. Code Examples

Customizing List Display

You can decide which fields to display on the change list page of the admin interface:

class YourModelAdmin(admin.ModelAdmin):
    list_display = ('field1', 'field2',)

admin.site.register(YourModel, YourModelAdmin)

Adding Filters

You can add filters to the change list page of the admin interface:

class YourModelAdmin(admin.ModelAdmin):
    list_filter = ('field1', 'field2',)

admin.site.register(YourModel, YourModelAdmin)

Creating Custom Admin Actions

You can create custom actions to be performed on selected objects:

class YourModelAdmin(admin.ModelAdmin):
    actions = ['custom_action']

    def custom_action(self, request, queryset):
        # Perform your action here
        pass

    custom_action.short_description = 'Custom Action'

admin.site.register(YourModel, YourModelAdmin)

4. Summary

We have covered how to set up and customize Django's admin interface, including how to create custom admin actions and how to customize the admin list page.

For further learning, you can look into Django's documentation on the admin site, which covers more advanced customization options.

5. Practice Exercises

Exercise 1: Create a custom admin interface for a blog post model. The list display should include the title, author, and date published. Add filters for the author and date published.

Solution:

class PostAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'date_published')
    list_filter = ('author', 'date_published')

admin.site.register(Post, PostAdmin)

Exercise 2: Add a custom action to the post model's admin interface that allows you to mark selected posts as published.

Solution:

class PostAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'date_published')
    list_filter = ('author', 'date_published')
    actions = ['mark_published']

    def mark_published(self, request, queryset):
        queryset.update(status='p')

    mark_published.short_description = 'Mark selected posts as Published'

admin.site.register(Post, PostAdmin)

Exercise 3: Create a custom admin form for the post model that includes validation.

Solution:

from django import forms

class PostAdminForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = '__all__'

    def clean_title(self):
        title = self.cleaned_data.get('title')
        if not title:
            raise forms.ValidationError('This field cannot be blank.')
        return title

class PostAdmin(admin.ModelAdmin):
    form = PostAdminForm

admin.site.register(Post, PostAdmin)

Happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Age Calculator

Calculate age from date of birth.

Use tool

URL Encoder/Decoder

Encode or decode URLs easily for web applications.

Use tool

Hex to Decimal Converter

Convert between hexadecimal and decimal values.

Use tool

XML Sitemap Generator

Generate XML sitemaps for search engines.

Use tool

Base64 Encoder/Decoder

Encode and decode Base64 strings.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help