update a html form in django

i want to update a form using only html form i need help please

from django.shortcuts import render, redirect
from .models import Todo
from django.shortcuts import render, get_object_or_404
from django.contrib import messages
# Create your views here.




def create(request):
    if request.method == 'POST':
        title = request.POST.get('title')
        description = request.POST.get('description')
        priority = request.POST.get('priority')
        completed = request.POST.get('completed') 
        if completed =='on':
            completed=True
        else:
            completed=False
        print('ssssssssssssssssss',completed)
        create_todo = Todo.objects.create(title=title, description=description, priority=priority, completed=completed)
        # if create_todo.is_valid():
        create_todo.save()
        return redirect('view_doto')
    return render(request, 'home/create.html')

def update(request, id):
    postobj= get_object_or_404(Todo, id=id)
    if request.method == 'POST':
        if request.POST.get('title') and request.POST.get('description') and request.POST.get('priority') and request.POST.get('completed') :
            Todo.objects.filter(id = id).update(title= request.POST.get('title'), description= request.POST.get('description'),  priority= request.POST.get('priority'),  completed= request.POST.get('completed'))
            messages.success(request, "The post was successfully updated")

            context={'postobj': postobj}
    else:
        context={'postobj': postobj,'error': 'The post was not successfully updated. The title and content must be filled out.'}
    # context={'postobj': postobj}
        return render(request, 'home/update.html',context)


def view_doto(request):
    posts = Todo.objects.all().order_by('-id')
    return render(request, 'home/index.html', {'posts':posts})

this is my views.py

{% extends "base.html" %}

{% block content %}

<div class="header-bar">
    <a href="{% url 'view_doto' %}">&#8592; Back</a>
</div>

<div class="card-body">
    <form method="POST"  action="{% url 'create' %}">{% csrf_token %}
       
        <div class="form-outline">
            <label class="form-label" for="formControlLg">Title</label>
            <input type="text" name="title" value="{{ postobj.title }}" class="form-control form-control-lg" />
          </div>
          
          <div class="form-outline">
            <label class="form-label" for="formControlDefault">Description</label>
            <input type="text" name="description" value="{{ postobj.description}}" class="form-control" />
            
          </div>
          
          <div class="form-outline">
            <label class="form-label" for="typeNumber">Priority</label>
            <input type="number" name="priority" value="{{postobj.priority}}" class="form-control" />
          </div><br>

          <div class="form-check">
            <input class="form-check-input" type="checkbox" value="{{ postobj.completed}}"  name="completed"> Completed
            <!-- <label class="form-check-label" for="flexCheckDefault"> -->
             
            </label>
          </div>
          
          <br>

          <input class="button" type="submit" value="search">
      </form>

</div>

{% endblock content%}

this is the template for the update…please how can i fix this i eed help

The easiest way to get it all working correctly would be to fix your template and view to use a Django form. It’s going to prevent a lot of problems going forward if you take the time now to do this the way that Django intends for it to be done.

thanks alot sir but can i use it in APIs too

What specifically do you mean? The example and question you posed is a standard POST form submission. How do APIs fit into this?

just asking if i can still use django forms during APIs

A Django form is a Python object representing an HTML form, along with a lot of functionality baked in.

That functionality “activated” when you either initially render the form to HTML as an unbound form, or when data is submitted via POST to a view which then binds the submitted data to that form.

It actually doesn’t matter what is sending or receiving that data as long as the view knows what to do with what is being sent to it.

1 Like

thanks sir i appreciate