Django : Update sqlite database from html template

I've a database table called 'FormDB. I've written a program for CURD operation. For each row of db data there're two buttons 'EDIT' and 'DELETE'. The edit button is functioning when i give <form action = "" method = "POST"> in the HTML template. but when I redirect to the page where all the db data is showing, it's creating a new row of data instead of updating the existing value.

view.py

def EditData(request, id):
a = FormDB.objects.get(id=id)
nameupdate = a.name
form = DjangoForm(request.POST)
if request.method == "POST" and form.is_valid():
    newname = form.cleaned_data['name']
    b = FormDB.objects.get(id=id)
    b.name = newname
    b.save()
return render(request, "editform.html", {'nameupdate' : nameupdate})

def ViewDBData(request):
editeddb = list(FormDB.objects.all().values())
return HttpResponseRedirect('/viewdb/')

editform.html

    <body>
    <h1>EDIT PAGE</h1>
    <form action = "/viewdb/" method = "POST">
        {% csrf_token %}
        {% if nameupdate %}
            <label>Enter new name: </label>
            <input type="text" name = "name" placeholder = {{ nameupdate }} REQUIRED>
            <input type="submit" value = "Submit Changes" ><br>
        {% endif %}
    </form>

urls.py

urlpatterns = [
...........
..........
path('viewdb/', ViewDBData),

]

So, when I click on Submit Changes, it should redirect to the page where all dbdata is showing with updated value.

I can't see how this code can create new row, but what you're doing is not just redirect. You are sending your form data to endpoint, that have no info about id. You should send form data to endpoint like editdata/{id} (I beleive it's what you're doing here <form action = "" method = "POST">). And do redirect inside EditData.

Also, if your DjangoForm is ModelForm then to save changes, you can do just this

form = DjangoForm(request.POST, instance=FormDB.objects.get(id=id))
form.save()
Вернуться на верх