WEEK 10 – THURSDAY
TOPIC: Registering Models (Showing Data in the Django Admin Panel)
Learning Objectives
At the end of this lesson, students should be able to:
Explain what a Django model is.
Explain the purpose of the Django Admin Panel.
Register a model in the Admin Panel.
View and manage model data through the Admin Panel.
Introduction
In the previous lesson, we created a Student model and stored student information in the MySQL database.
However, even though the data is stored in the database, Django will not display it in the Admin Panel until the model is registered.
Today, we will learn how to register a model so that it appears in the Django Admin Panel.
What is a Model?
A model is a Python class that represents a table in the database.
For example:
class Student(models.Model):
full_name = models.CharField(max_length=100)
email = models.EmailField()
course = models.CharField(max_length=100)
What we are doing
We created a model named Student. Django used this model to create a table named students_student in the database.
What is the Django Admin Panel?
The Django Admin Panel is a built-in website provided by Django for administrators.
It allows administrators to:
Add new records
View records
Edit records
Delete records
Manage users
Manage registered models
The default Admin Panel address is:
http://127.0.0.1:8000/admin/
Why Do We Register Models?
By default, Django does not show your models in the Admin Panel.
We register a model to tell Django:
"Display this model in the Admin Panel so administrators can manage its data."
STEP 1: Open the admin.py File
Open:
students/admin.py
What we are doing
We are opening the file responsible for registering models in the Django Admin Panel.
Initially, the file looks like this:
from django.contrib import admin
# Register your models here.
STEP 2: Import the Student Model
Add the following line:
from .models import Student
Your file now becomes:
from django.contrib import admin
from .models import Student
What we are doing
We are importing the Student model from the models.py file so that Django knows which model we want to register.
Meaning of the code
| Code | Meaning |
|---|---|
from | Bring something from another file |
. | Refers to the current app (students) |
models | The models.py file |
import | Bring into this file |
Student | The model we want to register |
STEP 3: Register the Model
Below the import statements, write:
admin.site.register(Student)
Your complete admin.py file should look like this:
from django.contrib import admin
from .models import Student
admin.site.register(Student)
What we are doing
We are telling Django:
"Display the Student model in the Admin Panel."
Meaning of the code
| Code | Meaning |
|---|---|
admin | Django's administration system |
site | Refers to the Django Admin website |
register() | Registers a model so it appears in the Admin Panel |
Student | The model to be displayed |
STEP 4: Start the Django Server
Run the following command:
python manage.py runserver
What we are doing
We are starting the Django development server so we can access the Admin Panel through a web browser.
STEP 5: Open the Admin Panel
Open your browser and visit:
http://127.0.0.1:8000/admin/
What we are doing
We are opening Django's built-in administration website.
STEP 6: Log In
Enter the superuser username and password you created earlier.
Example:
Username: admin
Password: ********
What we are doing
We are signing in as the administrator to access and manage application data.
STEP 7: View the Student Model
After logging in, you should see a section named Students.
Click Students.
What we are doing
We are opening the Student model to view all student records stored in the database.
If no records have been added yet, the list will be empty.
STEP 8: Add a New Student
Click Add Student.
Fill in the fields:
Full Name
Email
Course
Click Save.
What we are doing
We are creating a new student record using the Django Admin Panel. Django saves the information directly into the students_student table in the MySQL database.
STEP 9: Edit a Student Record
Click on any student's name.
Update the information.
Click Save.
What we are doing
We are modifying an existing record in the database.
STEP 10: Delete a Student Record
Select a student record.
Click Delete and confirm.
What we are doing
We are removing the selected record from the database.
Summary
Create Student model
↓
Open admin.py
↓
Import Student model
↓
Register the model
↓
Run the server
↓
Open http://127.0.0.1:8000/admin/
↓
Log in as superuser
↓
Manage student records through the Admin Panel
Class Exercise
Register the
Studentmodel inadmin.py.Log in to the Django Admin Panel.
Add three student records.
Edit one student's course.
Delete one student record.
Verify that the changes are reflected in the MySQL database by running:
SELECT * FROM students_student;
This exercise helps students understand how Django's Admin Panel provides a simple interface for managing database records without writing SQL queries manually.


0 comments:
Post a Comment