In Django, the admin interface is a powerful tool for managing the database records. It provides an easy-to-use interface for CRUD (Create, Read, Update, Delete) operations. However, there might be times when you need to extend the default functionality of the admin site and add custom views or actions.
Custom admin views allow you to create additional pages with custom functionalities in the admin interface. These views can be utilized to perform specific tasks that are not covered by the default admin pages. Here's how you can create a custom admin view:
admin.py
file of your Django app.from django.contrib import admin
and from django.urls import path
.admin.site.register()
line, define your custom view as a function. Make sure to include the request
parameter.@admin.site.admin_view
.path
function and specify the corresponding URL pattern.HttpResponse
object to return the desired content.from django.contrib import admin
from django.urls import path
from django.http import HttpResponse
@admin.site.admin_view
def custom_view(request):
return HttpResponse("This is a custom admin view.")
urlpatterns = [
# Other URL patterns...
path('admin/custom/', custom_view, name='custom_admin_view'),
]
After implementing the above steps, you will have a new URL pattern available at admin/custom/
, which will display the content returned by your custom view. You can access this view only if you are logged in as a superuser or have the necessary permissions.
Admin actions provide a way to perform batch operations on selected database records. Django admin includes default actions like delete selected, but you can create your own custom actions catering to your specific needs. Follow the steps below to create a custom admin action:
admin.py
file of your Django app.from django.contrib import admin
and from django.contrib import messages
.modeladmin
, request
, and queryset
.short_description
attribute of the function.queryset
to perform operations on the selected records.messages.success()
function to display a success message. Remember to import the messages module.actions
attribute of the admin model.from django.contrib import admin
from django.contrib import messages
def mark_as_published(modeladmin, request, queryset):
queryset.update(status='published')
messages.success(request, "Selected records marked as published.")
mark_as_published.short_description = "Mark selected items as published"
class ArticleAdmin(admin.ModelAdmin):
actions = [mark_as_published]
admin.site.register(Article, ArticleAdmin)
After adding the custom action to the actions
attribute, you will see a new action option in the admin interface's action dropdown for the specified model. Selecting this action and clicking "Go" will execute the defined logic on the selected records.
Creating custom admin views and actions in Django allows you to extend the functionality of the admin interface according to your project requirements. With custom views, you can add additional pages for performing specific tasks. Custom actions enable you to perform batch operations on selected records easily. By leveraging these features, you can tailor the Django admin site to better meet your application's needs.
noob to master © copyleft