π Introduction
Django provides a powerful Admin Interface that allows you to:
- view records
- add new data
- update records
- delete records
π But for this, the model must be registered in admin.py.
π― Program Statement
π Register a model in Django admin and display records.
π§ Concept
This program uses:
models.pyβ database structureadmin.pyβ model registration- Django admin panel
βοΈ Step 1: Model (Already Created)
π File: models.py
πΉ Path:
myproject/myapp/models.py
πΉ Code:
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
course = models.CharField(max_length=100)
email = models.EmailField(unique=True)
def __str__(self):
return self.name
βοΈ Step 2: Register Model in Admin
π File: admin.py
πΉ Path:
myproject/myapp/admin.py
πΉ Code:
from django.contrib import admin
from .models import Student
admin.site.register(Student)
π§ Explanation
admin.site.register(Student)makes the model visible in admin panel- Without this, model will not appear in admin
βοΈ Step 3: Run Server
python manage.py runserver
βοΈ Step 4: Open Admin Panel
π Open:
http://127.0.0.1:8000/admin/
βοΈ Step 5: Login
π Use superuser credentials
(If not created, see next post π)
π Step 6: Output
After Login:
You will see:
MYAPP
Students
Click βStudentsβ
π You will see:
- list of student records
- Add button
- Edit option
Click βAdd Studentβ
Form appears like:
Name: ______
Age: ______
Course: ______
Email: ______
After Saving:
Records will be displayed in admin list.
π§ How It Works
- Model defines database table
- Admin registers the model
- Django automatically generates admin UI
- You can perform CRUD operations directly
π₯ Key Concepts
Register Model
admin.site.register(Student)
Admin Panel URL
/admin/
Model Display
def __str__(self):
return self.name
Controls how record appears in admin.
β οΈ Common Errors
β Model not visible in admin
π Forgot:
admin.site.register(Student)
β No superuser
π Cannot login
β str not defined
π Admin shows object like:
Student object (1)
β Server not running
π Admin page will not open
π§ͺ Practice Questions
- Register another model
- Add more fields in Student model
- Modify
__str__method - Add multiple records via admin
- Try editing records
π€ Viva Questions & Answers
1. What is Django admin?
Django admin is a built-in interface that allows developers to manage database records easily.
2. Why do we register a model in admin?
To make the model visible and manageable in the admin interface.
3. What is admin.site.register()?
It registers a model with the Django admin site.
4. What is the purpose of admin panel?
To perform CRUD operations without writing frontend code.
5. What is __str__() method?
It defines how an object is displayed in admin.
6. Can we add records using admin?
Yes, admin provides Add, Edit, and Delete functionality.
7. What happens if model is not registered?
It will not appear in admin panel.
8. What URL opens admin panel?
/admin/
9. Who can access admin panel?
Only authenticated users (superuser/staff).
10. Why is admin important?
It saves time by providing automatic backend interface.
π Navigation
π Next Post: Customize Admin Interface (list_display, search_fields, filters)
π Back to List: Django Programs (60 Questions with Solutions)
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
