beginner#Django#Admin Interface#Model Management
Using Django's Built-in Admin Interface
Learn how to use Django's built-in admin interface to manage models and data.
Introduction to Django Admin
Django comes with a built-in admin interface that allows you to manage models and data. In this tutorial, we will explore how to use Django's admin interface.
Step 1: Create a Superuser
Create a superuser to access the admin interface:
python manage.py createsuperuser
Step 2: Register Models with the Admin Interface
Register models with the admin interface by creating an Admin class:
from django.contrib import admin
from .models import Book
class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'author')
search_fields = ('title', 'author')
admin.site.register(Book, BookAdmin)
Step 3: Customize the Admin Interface
Customize the admin interface by adding custom views and templates:
from django.contrib import admin
from .models import Book
class BookAdmin(admin.ModelAdmin):
change_list_template = 'admin/book_change_list.html'
admin.site.register(Book, BookAdmin)
Step 4: Add Actions to the Admin Interface
Add actions to the admin interface to perform custom tasks:
from django.contrib import admin
from .models import Book
class BookAdmin(admin.ModelAdmin):
actions = ['make_published']
def make_published(self, request, queryset):
queryset.update(status='published')
make_published.short_description = 'Make selected books published'
admin.site.register(Book, BookAdmin)