Admin
Admin: Today we’re going to:
- Activate it completely
- Make it beautiful and useful for your polls app
- Customize list views, filters, search, inline editing, actions, etc.
- Add superuser + understand permissions basics
- Show real-world tips that separate “looks okay” from “production-ready admin”
Let’s build it step by step like I’m sitting next to you.
Step 1: Is Admin Already Enabled? (Quick Check)
Open mysite/settings.py
Look for INSTALLED_APPS — you should see these already there (they come by default):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
INSTALLED_APPS = [ 'django.contrib.admin', # ← this one! 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', ... 'polls.apps.PollsConfig', 'pages.apps.PagesConfig', ] |
Also check mysite/urls.py:
|
0 1 2 3 4 5 6 7 8 9 |
urlpatterns = [ path('admin/', admin.site.urls), # ← this line! ... ] |
If both are there → admin is already technically enabled.
Step 2: Create a Superuser (Admin Login)
Run this once (only needed first time):
|
0 1 2 3 4 5 6 |
python manage.py createsuperuser |
Example session:
|
0 1 2 3 4 5 6 7 8 9 10 |
Username: webliance Email address: webliance@example.com Password: ******** Password (again): ******** Superuser created successfully. |
Now start server:
|
0 1 2 3 4 5 6 |
python manage.py runserver |
Go to: http://127.0.0.1:8000/admin/
Login with the credentials you just created.
→ You should see the default admin dashboard (but no Polls yet!)
Step 3: Register Your Models (Make Polls Visible)
The admin is empty because you haven’t told it about your models yet.
Open polls/admin.py (it’s almost empty by default)
Replace everything with this (modern, clean style):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
from django.contrib import admin from .models import Question, Choice # ── Very nice inline editing for choices ──────────────────────────────── class ChoiceInline(admin.TabularInline): model = Choice extra = 3 # show 3 empty rows ready to fill fields = ['choice_text', 'votes'] ordering = ['id'] @admin.register(Question) class QuestionAdmin(admin.ModelAdmin): # Main list view columns list_display = ( 'question_text', 'category', 'pub_date', 'was_published_recently', # your custom method! 'is_active', 'slug', ) # Clickable fields → go to change page list_display_links = ('question_text',) # Filter sidebar list_filter = ( 'category', 'is_active', 'pub_date', ) # Search box (searches these fields) search_fields = ( 'question_text', 'slug', ) # Show choices right inside question edit page inlines = [ChoiceInline] # Fieldsets – group fields nicely fieldsets = ( (None, { 'fields': ('question_text', 'slug') }), ('Publication & Status', { 'fields': ('pub_date', 'is_active', 'category'), 'classes': ('collapse',) # collapsible section }), ) # Pre-populate slug from question_text prepopulated_fields = {'slug': ('question_text',)} # Date hierarchy (nice calendar filter) date_hierarchy = 'pub_date' # How many items per page list_per_page = 20 @admin.register(Choice) class ChoiceAdmin(admin.ModelAdmin): list_display = ( 'choice_text', 'question', 'votes', ) list_filter = ('question',) search_fields = ('choice_text',) # Raw ID widget for foreign key (useful when many questions) raw_id_fields = ('question',) |
Save → refresh http://127.0.0.1:8000/admin/
Now you should see:
- “Questions” and “Choices” under Polls section
- Nice list views with filters, search, inline choices when editing question
- Slug auto-filled when creating new question
Step 4: Real-World Polish Touches (What Pros Do)
Add these one by one to QuestionAdmin:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# Show green/red icon for recently published def was_published_recently(self, obj): if obj.was_published_recently(): return "🟢 Yes" return "🔴 No" was_published_recently.short_description = "Recent?" was_published_recently.boolean = True # shows as nice icon in some themes # Custom admin actions (bulk operations) @admin.action(description="Mark selected questions as active") def make_active(modeladmin, request, queryset): queryset.update(is_active=True) @admin.action(description="Mark selected questions as inactive") def make_inactive(modeladmin, request, queryset): queryset.update(is_active=False) actions = ['make_active', 'make_inactive'] |
Now you can select multiple questions → Actions dropdown → Mark active/inactive.
Step 5: Quick Test Drive
- Go to /admin/
- Login
- Click “Questions” → see list
- Click “Add Question” → see nice form + 3 empty choice rows
- Fill question + category + 2–3 choices → Save
- Back to list → see your new question + recent icon
- Select it + one old → Actions → make inactive → confirm
Bonus: Make Admin Feel Modern (2026 Touch)
Many teams add these tiny improvements:
-
Change admin title
In mysite/admin.py (create if missing):
Python012345678admin.site.site_header = "Hyderabad Polls Administration"admin.site.site_title = "Polls Admin"admin.site.index_title = "Welcome to Polls Backend" -
Add favicon (later with static files)
-
Use django-jazzmin or django-admin-interface package (very popular in India 2026)
Bash0123456pip install django-jazzminThen add ‘jazzmin’ before ‘django.contrib.admin’ in INSTALLED_APPS
→ Beautiful sidebar, dark mode, charts, etc.
Common Beginner Pain Points
| Problem | Likely Cause | Fix |
|---|---|---|
| No “Polls” section | Forgot admin.site.register(Question) | Add @admin.register or admin.site.register |
| No choices inline | Forgot inlines = [ChoiceInline] | Add the inline class |
| Slug not auto-filling | No prepopulated_fields | Add prepopulated_fields = {‘slug’: (‘question_text’,)} |
| Can’t see old questions | ordering in Meta or list ordering | Add ordering = [‘-pub_date’] in model Meta |
| “You don’t have permission” | Not superuser or wrong user | Login with superuser or give staff permissions |
Your Quick Homework
- Add the full admin.py code shown above
- Create 4–5 questions + choices via admin
- Play with filters, search, bulk actions
- Try marking some inactive → see them disappear from frontend list (if you filtered is_active=True)
- (Optional) Install django-jazzmin → see how pretty it becomes
Tell me what feels next:
- “Admin looks great! Now show me permissions & staff users”
- “How to add charts or custom admin pages?”
- “I want to customize change list even more (custom columns, CSS)”
- “Got error when saving – here’s message”
- Or finally ready for: “Let’s finish the voting system – form + POST + F() + results”
You now have a production-grade admin backend in ~30 lines of code — this is why people fall in love with Django. You’re doing fantastic — let’s keep building! 🚀🇮🇳
