Django Admin - potatoscript/django GitHub Wiki
π© What Is Django Admin?
Django Admin is a ready-to-use web interface that lets you manage your appβs data in a super professional way β just like a boss! π§βπΌ
β You can:
- Add, edit, or delete database records π
- Manage users and permissions π
- Preview your models like magic πͺ
πͺ Step 1: Creating a Superuser (Admin Account)
Youβll need an account with superpowers to log in to the Admin Panel.
Open your terminal:
python manage.py createsuperuser
π§ Django will ask:
Username: lucyberry
Email address: [email protected]
Password: *********
π₯ Boom! You're an admin now.
π Step 2: Start the Server & Visit the Admin
Run the server:
python manage.py runserver
Now visit:
π http://127.0.0.1:8000/admin
β¨ You'll see a login screen. Enter your superuser credentials and tada!
Youβre inside the Django Control Room π
π Step 3: Register Your Models
By default, your models donβt show up in the Admin Panel. You have to register them.
Letβs say we have a model like this:
π models.py
class Toy(models.Model):
name = models.CharField(max_length=100)
brand = models.CharField(max_length=100)
price = models.DecimalField(max_digits=6, decimal_places=2)
β‘οΈ Go to admin.py
in the same app and add:
from django.contrib import admin
from .models import Toy
admin.site.register(Toy)
π Refresh your Admin Panel β and you'll see Toys
in the sidebar!
π¬ Step 4: Customize the Admin Panel (Make It Fancy)
Letβs make our Admin interface more informative and user-friendly!
π admin.py
class ToyAdmin(admin.ModelAdmin):
list_display = ('name', 'brand', 'price') # Show columns in list view
search_fields = ('name', 'brand') # Add a search bar
list_filter = ('brand',) # Add filters on the side
admin.site.register(Toy, ToyAdmin)
π Now your admin panel is searchable, filterable, and beautiful!
ποΈβπ¨οΈ Step 5: Add More Models
Letβs say you also have a Review
model.
class Review(models.Model):
toy = models.ForeignKey(Toy, on_delete=models.CASCADE)
reviewer_name = models.CharField(max_length=100)
rating = models.IntegerField()
comment = models.TextField()
Register it like this:
class ReviewAdmin(admin.ModelAdmin):
list_display = ('reviewer_name', 'rating', 'toy')
list_filter = ('rating',)
admin.site.register(Review, ReviewAdmin)
π§ Bonus: Inline Editing
Want to edit reviews inside the Toy edit screen? You can!
class ReviewInline(admin.TabularInline): # Or use StackedInline
model = Review
extra = 1 # Number of empty forms
class ToyAdmin(admin.ModelAdmin):
inlines = [ReviewInline]
admin.site.register(Toy, ToyAdmin)
π Now when you edit a Toy, youβll also see reviews listed on the same page.
π¨βπ» Whatβs Inside the Admin Panel?
Section | Purpose |
---|---|
βοΈ Authentication & Authorization | Manage users and groups |
βοΈ Your Registered Models | Add/edit/delete data easily |
βοΈ Filters/Search | Makes navigation easy |
βοΈ User Permissions | Control who can do what |
π User Access and Groups
Inside Admin, you can:
- Add new users (without coding!)
- Assign them to groups
- Give different permissions
Example:
- A "Moderator" group can view Reviews but cannot delete them.
- An "Editor" can update Toys but not change users.
π Summary Table
Task | Command or Action |
---|---|
π€ Create admin user | python manage.py createsuperuser |
π Run server | python manage.py runserver |
π Visit Admin Panel | http://127.0.0.1:8000/admin |
π¦ Register models | Use admin.site.register() in admin.py |
π οΈ Customize Admin | Use ModelAdmin classes |
π Inline editing | Use TabularInline or StackedInline |