Python class-based-view updateview not get saved

Question:

I am new in programming and trying to make a website for Todo by using Python-django.
I am using Django Class Based Update View to make edit in datas.
But while I am click on submit button its not get saved.

models.py

class Task(models.Model):
name=models.CharField(max_length=25)
details=models.CharField(max_length=750)
priority=models.CharField(max_length=500)
date=models.DateField()
user=models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

def __str__(self):
    return self.name

views.py

class TaskUpdateView(UpdateView):
model = Task
fields = "__all__"
template_name = 'update.html'
context_object_name = 'task'
success_url = reverse_lazy('cbvhome')

urls.py

path('update/<pk>',views.TaskUpdateView.as_view(),name='cbvupdate')

update.html

<form method="post" class=" justify-content-center align-items-center mb-4 ps" enctype="multipart/form-data">
                    {% csrf_token %}
                    <div class="mb-3">
                      <label for="task-id" class="form-label  me-5">Task</label>
                      <input type="text" name="name" class="form-control" id="task-id" aria-describedby="emailHelp" style="width: 80%;" placeholder="Enter your Task Here" value="{{task.name}}">
                    </div>
                    <div class="mb-3">
                        <label for="exampleFormControlTextarea1" class="form-label me-5">Enter the details</label>
                        <textarea class="form-control" name="details" id="task-detail"rows="2" placeholder="Enter the task details" style="width: 80%;">{{task.details}}</textarea>
                        
                    </div>
                    <div class="mb-3">
                      <label for="task-date" class="form-label  me-5">Date set curerntly: {{task.date}}</label>
                      <input type="date" name="date" class="form-control" id="task-date" style="width: 80%;" value="{{task.date}}">
                    </div>
                    <div class="mb-3 mt-3">
                        <label for="task-prio" class="form-label  me-5">Select the priority</label>
                        <select class="form-select" name="priority" aria-label="Default select example" style="width: 80%;" id="task-prio" value="{{task.priority}}">
                            <option selected>{{task.priority}}</option>
                            <option value="Very Urgent" class="text-danger">Very Urgent</option>
                            <option value="Urgent" class="text-warning">Urgent</option>
                            <option value="Important" class="text-primary">Important</option>
                          </select>
                    </div>
                    <div class="submit pe-5" style="padding-left: 100px; padding-top: 10px;" >
                    <input type="submit" class="btn btn-outline-success me-5" value="Save">
                    </div>    
                </form>
Asked By: Shakeeb Kp

||

Answers:

By default if nothing is given in url params, so it is considered as str type, so define it as int:

path('update/<int:pk>/',views.TaskUpdateView.as_view(),name='cbvupdate')

It will be better if you give action="{% url 'cbvupdate' %}" in form tag of HTML although not necessary as Django always takes current page route.

Note: Always add / at the end of every route.

Answered By: Sunderam Dubey