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.
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.
Django Admin can save development time because it provides many common database management features automatically.
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/
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.
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.
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.
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.
During superuser creation, Django asks you to provide a username.
Username: admin
You can choose an appropriate username according to your project.
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.
Django asks you to enter and confirm a password.
Password:
Password (again):
Choose a strong password and keep it secure.
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/
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.
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)
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.
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.
After registering the Student model, an authorized administrator can use the admin interface to add student records.
For example:
Django generates the corresponding form based on the model fields.
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.
The admin interface can also provide options to delete records.
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)
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
)
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.
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.
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',
)
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.
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',
)
}),
)
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.
Django groups allow permissions to be assigned to multiple users.
For example, a project could create groups such as:
The exact permissions can be configured according to the application's needs.
The Django admin can be customized in many ways, including:
This allows the built-in admin interface to fit the needs of a project.
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.
The Django Admin Panel provides a ready-made interface for managing registered Django models.
/admin/.createsuperuser to create an administrative account.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.python manage.py createsuperuser to create a superuser.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.Question: Which command is used to create a Django superuser?