Django Admin

Powerful built-in administration interface

🎯 What is Django Admin?

Django Admin is a powerful, ready-to-use interface for managing your application's data. It automatically generates admin pages for your models, allowing you to create, read, update, and delete records effortlessly.


# Access admin at: http://127.0.0.1:8000/admin/
from django.contrib import admin
from .models import Member

admin.site.register(Member)
                                    

Key Admin Features

🔐

Authentication

Secure login system built-in

python manage.py createsuperuser
📊

Data Management

CRUD operations for all models

admin.site.register(MyModel)
🎨

Customizable

Tailor admin interface to needs

class MyAdmin(admin.ModelAdmin):
    pass
🔍

Search & Filter

Find records quickly and easily

search_fields = ['name']

🔹 Creating a Superuser

Before accessing the admin interface, you need to create a superuser account. This account has full permissions to manage all data and settings in your Django application.

# Run this command in your terminal
python manage.py createsuperuser

# You'll be prompted to enter:
# Username: admin
# Email: [email protected]
# Password: ********
# Password (again): ********

After creating superuser:

  1. Start your development server: python manage.py runserver
  2. Visit: http://127.0.0.1:8000/admin/
  3. Login with your superuser credentials

🔹 Registering Models

To manage your models through the admin interface, you must register them in your app's admin.py file. This makes your models visible and editable in the admin panel.

# myapp/admin.py
from django.contrib import admin
from .models import Member, Product, Category

# Simple registration
admin.site.register(Member)

# Register multiple models
admin.site.register([Product, Category])

🔹 Basic Admin Customization

Customize how your models appear in the admin interface using ModelAdmin classes. You can control which fields are displayed, add search functionality, and configure filters for better data management.

# myapp/admin.py
from django.contrib import admin
from .models import Member

class MemberAdmin(admin.ModelAdmin):
    list_display = ('firstname', 'lastname', 'joined_date')
    search_fields = ['firstname', 'lastname']
    list_filter = ['joined_date']

admin.site.register(Member, MemberAdmin)

🔹 Admin Site Configuration

Personalize your admin interface by changing the site header, title, and index title. This helps brand your admin panel and make it more recognizable for your team.

# myapp/admin.py
from django.contrib import admin

# Customize admin site
admin.site.site_header = "My Project Admin"
admin.site.site_title = "Admin Portal"
admin.site.index_title = "Welcome to My Project Admin"

🧠 Test Your Knowledge

What command creates a superuser in Django?