Reference Section 19: Custom Dashboard / Project Scaling
Edit Category
urls.py of Dashboards app
```
urlpatterns = [
path('', views.dashboard, name='dashboard'),
path('categories/', views.categories, name='categories'),
path('categories/add/', views.add_category, name='add_category'),
path('categories/edit/<int:pk>/', views.edit_category, name='edit_category'),
]
```
views.py of the Dashboards app
```
def edit_category(request, pk):
return render(request, 'dashboard/edit_category.html')
```
edit_category.html
```
<div class="col-md-9">
<h3>Edit Category</h3>
<form action= " " method = "POST" style="width: 500px;">
{% csrf_token %}
{{form | crispy}}
<button type="submit" class="btn btn-warning"> submit</button>
```
categories.html
```
<tbody>
{% for cat in categories %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{ cat }}</td>
<td>{{ cat.created_at }}</td>
<td>{{ cat.updated_at }}</td>
<a href="{% url 'edit_category' cat.id %}"><td><i class="fa fa-edit text-success"></i></td></a>
<a href=" "><td><i class="fa fa-trash text-danger"></i>
</td></a>
</tr>
{% endfor %}
</tbody>
````
Hi Ejaz Ali Inayat,
Your code is correct, but there is a small mistake in the views.py
of the Dashboards app. You need to pass the form as context to the template. If you don't pass the context, you'll encounter an error because the template expects the form
variable.
@mohaiminul-islam Sir, I don't understand well. Kindly guide me on how I pass the form as context to the template.
Kindly clarify me. thanks
Ejaz Ali Inayat,
If you want to edit a category, you first need to create a form for it. You can do this in your forms.py
file:
class CategoryForm(forms.ModelForm): class Meta: model = Category fields = ['name']
After that, in your views.py
, you need to get the instance of the category using its primary key (pk
). Then, you pass this instance to your form and render the form in your template. Here’s how you can do it:
# views.py from django.shortcuts import render, get_object_or_404, redirect from .models import Category from .forms import CategoryForm def edit_category(request, pk): category = get_object_or_404(Category, pk=pk) if request.method == 'POST': form = CategoryForm(request.POST, instance=category) if form.is_valid(): form.save() return redirect('categories') # Redirect to the categories page after a successful update else: form = CategoryForm(instance=category) return render(request, 'dashboard/edit_category.html', {'form': form})
With get_object_or_404
, we fetch the category data from the database. If the category doesn’t exist, it will show a "404 Not Found" error. Then, we pass the category instance to the form, which is passed to the template. When the user updates the category and submits the form, the POST
request updates the category and redirects to the categories page.
This code is an example and can be customized to fit your needs. If you have any confusion, feel free to ask, and I’ll try to explain it in more detail.
@mohaiminul-islam Sir I tried to solve the issue, but the problem remains the same. The GitHub link is as follows,
https://github.com/webextolcollege/django-blog.git
Kindly instruct me, where I made a mistake
I ran your code on my system and noticed that you haven't implemented the category edit function. I suggest watching the tutorial carefully to understand how it works and learn how to create it. If you need any help, you can refer to our official GitHub repository using this link:
GitHub - Django Blog Project
If you encounter any issues, please mention them clearly and provide proper screenshots of the problem. This will help me understand your issue better and provide an appropriate solution
@mohaiminul-islam Sir, Now I have updated the code on GitHub
The GitHub link is as follows,
https://github.com/webextolcollege/django-blog.git
Kindly instruct me, where I made a mistake
The issue you are facing is because of a mistake in your categories.html
file between lines 31 and 33. You have written the following code:
<a href="{% url 'edit_category' cat.id %}"><td><i class="fa fa-edit text-success"></i></td></a> <a href=" "><td><i class="fa fa-trash text-danger"></i> </td></a>
Because of this, the <a>
tag is affecting the HTML structure, which is why it is not working as expected. Instead of this code, you should use the following:
<td> <a href="{% url 'edit_category' cat.id %}"><i class="fa fa-edit text-success"></i></a> <a href=" "><td><i class="fa fa-trash text-danger"></i> </td>
This updated code should resolve the issue. If you encounter any further problems, feel free to let me know.
@mohaiminul-islam, A lot of thanks Sir, issue solved