Home > Back-end >  Python class-based-view updateview not get saved
Python class-based-view updateview not get saved

Time:10-08

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"  enctype="multipart/form-data">
                    {% csrf_token %}
                    <div >
                      <label for="task-id" >Task</label>
                      <input type="text" name="name"  id="task-id" aria-describedby="emailHelp" style="width: 80%;" placeholder="Enter your Task Here" value="{{task.name}}">
                    </div>
                    <div >
                        <label for="exampleFormControlTextarea1" >Enter the details</label>
                        <textarea  name="details" id="task-detail"rows="2" placeholder="Enter the task details" style="width: 80%;">{{task.details}}</textarea>
                        
                    </div>
                    <div >
                      <label for="task-date" >Date set curerntly: {{task.date}}</label>
                      <input type="date" name="date"  id="task-date" style="width: 80%;" value="{{task.date}}">
                    </div>
                    <div >
                        <label for="task-prio" >Select the priority</label>
                        <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" >Very Urgent</option>
                            <option value="Urgent" >Urgent</option>
                            <option value="Important" >Important</option>
                          </select>
                    </div>
                    <div  style="padding-left: 100px; padding-top: 10px;" >
                    <input type="submit"  value="Save">
                    </div>    
                </form>

CodePudding user response:

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.

  • Related