Technology Encyclopedia Home >How to build an admin backend with Django?

How to build an admin backend with Django?

To build an admin backend with Django, follow these steps:

  1. Set Up a Django Project
    First, install Django and create a new project:

    pip install django  
    django-admin startproject myadminbackend  
    cd myadminbackend  
    
  2. Create a Django App
    Inside your project, create an app where your models and admin logic will reside:

    python manage.py startapp myapp  
    
  3. Define Models
    In myapp/models.py, define the database models you want to manage in the admin backend. For example:

    from django.db import models  
    
    class Product(models.Model):  
        name = models.CharField(max_length=100)  
        price = models.DecimalField(max_digits=10, decimal_places=2)  
        stock = models.IntegerField()  
    
        def __str__(self):  
            return self.name  
    
  4. Register Models in Admin
    In myapp/admin.py, register your models to make them editable in the admin panel:

    from django.contrib import admin  
    from .models import Product  
    
    admin.site.register(Product)  
    
  5. Run Migrations
    Apply migrations to create the database tables:

    python manage.py makemigrations  
    python manage.py migrate  
    
  6. Create a Superuser
    A superuser can access the admin backend. Create one with:

    python manage.py createsuperuser  
    

    Follow the prompts to set a username, email, and password.

  7. Run the Development Server
    Start the server and access the admin panel at /admin:

    python manage.py runserver  
    

    Open a browser and go to http://127.0.0.1:8000/admin, then log in with your superuser credentials.

  8. Customize the Admin Backend (Optional)
    You can enhance the admin interface by customizing admin.py. For example, adding filters, search fields, or custom forms:

    @admin.register(Product)  
    class ProductAdmin(admin.ModelAdmin):  
        list_display = ('name', 'price', 'stock')  
        list_filter = ('stock',)  
        search_fields = ('name',)  
    

For Production & Scalability:
If you're deploying this admin backend for a business or enterprise, consider using Tencent Cloud's Cloud Database (TencentDB) for reliable data storage, Tencent Cloud Server (CVM) for hosting, and Tencent Cloud Object Storage (COS) for media files. Additionally, Tencent Cloud Load Balancer (CLB) can help distribute traffic efficiently.

This setup provides a secure, scalable, and manageable admin backend using Django.