To edit a Category, we first retrieve the existing record from the database.
category = Category.objects.get(Id=id)
The retrieved object is passed to the Model Form using the instance parameter.
form = CategoryForm(instance=category)
instance = category
The instance parameter tells Django which record should be edited. Without it, Django creates a new record instead of updating the existing one.
After submitting the edited form, Django updates the existing record in the database.
def editCategory(request,id):
category = Category.objects.get(Id=id)
if request.method == "POST":
form = CategoryForm(
request.POST,
instance=category
)
if form.is_valid():
form.save()
return redirect('categories')
else:
form = CategoryForm(
instance=category
)
return render(
request,
"editcategory.html",
{
"form":form
}
)
Create a template to display the Category Form with the existing data.
demo/templates/editcategory.html
{% extends 'base.html' %}
{% block content %}
<div class="container mt-4">
<h2>Edit Category</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit"
class="btn btn-warning">
Update
</button>
</form>
</div>
{% endblock %}
Django allows us to delete a record using the delete() method.
demo/views.py
from django.shortcuts import redirect
from .models import Category
def deleteCategory(request,id):
category = Category.objects.get(Id=id)
category.delete()
return redirect('categories')
| Code | Purpose |
|---|---|
| Category.objects.get() | Retrieve Existing Record |
| delete() | Delete Record |
| redirect() | Return to Category List |
category.delete()
The delete() method permanently removes the selected record from the corresponding database table.
Before deleting a record, it is recommended to ask the user for confirmation.
<a href="/deletecategory/{{ item.Id }}"
onclick="return confirm('Are you sure you want to delete this category?')"
class="btn btn-danger">
Delete
</a>
confirm("Are you sure?")
The JavaScript confirm() function displays a confirmation dialog.
| User Action | Result |
|---|---|
| OK | Delete the Record |
| Cancel | Stop the Delete Operation |
Now let us create a Category Management page where users can view, edit and delete records.
demo/templates/categories.html
{% extends 'base.html' %}
{% block content %}
<div class="container mt-4">
<h2>Category Management</h2>
<a href="/createcategory/"
class="btn btn-success mb-3">
Add Category
</a>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Id</th>
<th>Category Name</th>
<th width="180">Action</th>
</tr>
</thead>
<tbody>
{% for item in categories %}
<tr>
<td>{{ item.Id }}</td>
<td>{{ item.Name }}</td>
<td>
<a href="/editcategory/{{ item.Id }}"
class="btn btn-warning btn-sm">
Edit
</a>
<a href="{% url 'deletecategory' item.Id %}"
class="btn btn-danger btn-sm"
onclick="return confirm('Are you sure you want to delete this category?')">
Delete
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock %}
Instead of hardcoding URLs, Django provides the {% url %} template tag.
{% url 'deletecategory' item.Id %}
If the URL pattern changes in the future, only urls.py needs to be updated. The template code remains unchanged.
path(
'editcategory/<int:id>',
views.editCategory,
name='editcategory'
),
path(
'deletecategory/<int:id>',
views.deleteCategory,
name='deletecategory'
),
Category.objects.get()
CategoryForm(instance=category)
form.save()
category.delete()
redirect()
Select Record
↓
Edit Form
↓
Update Record
↓
Delete Record
↓
Category Management
Name
Price
Unit