i have a django website where it include an update function that filters the data based on the selected ID and returns all the related values of the requested record, where the user is allow to edit data and fields can't be empty.
the update is done only if all the fields are edited
my question is how to do the update even if just one field is edited
views.py
def update(request,pk):
#deny anonymouse user to enter the create page
if not request.user.is_authenticated:
return redirect("login")
else:
dbEntry = suspect.objects.get(pk =pk)
print( "db entry : ",dbEntry)
if request.method == 'POST':
first_name = request.POST['fname']
last_name = request.POST['lname']
fatherName = request.POST['fatherName']
motherName = request.POST['motherName']
gender = request.POST['gender']
content = request.POST['content']
dbEntry = suspect.objects.filter(pk = pk).first()
if dbEntry:
dbEntry.suspect_name = first_name
dbEntry.suspect_last_name = last_name
dbEntry.suspect_father_name = fatherName
dbEntry.suspect_mother_name = motherName
dbEntry.gender = gender
dbEntry.content = content
dbEntry.save()
return redirect("list")
print( "db entry after update: ",dbEntry)
else:
raise Http404('Id not found')
return render(request,'blog/update.html', {"dbEntry":dbEntry})
update.html
{% extends "base.html" %}
{% load static %}
{% block body %}
<head>
<link rel="stylesheet" type="text/css" href="{% static '/css/linesAnimation.css' %}">
<link rel="stylesheet" type="text/css" href="{% static '/css/input-lineBorderBlue.css' %}">
<link rel="stylesheet" type="text/css" href="{% static '/css/dropDown.css' %}">
<link rel="stylesheet" type="text/css" href="{% static '/css/home.css' %}">
<link rel="stylesheet" type="text/css" href="{% static '/css/meta-Input.css' %}">
<meta name= "viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="{% static '/js/jquery-3.1.1.min.js'%}"></script>
<title>Welcome</title>
</head>
<body>
<div class="lines">
<div class="line"></div><div class="line"></div>
<div class="line"></div><div class="line"></div>
<div class="line"></div><div class="line"></div><div class="line"></div>
</div>
<form method = "POST" enctype="multipart/form-data">
{% csrf_token %}
<div id='left-column-Input' class="formInput" include="select()">
<div class="forminputs">
<input type="text" id="fname" name="fname" autocomplete="off" required />
<label for="fname" class="label-name">
<span class="content-name" name="fname">{{dbEntry.suspect_name}}</span>
</label></div>
<div class="forminputs">
<input type="text" id="lname" name="lname" autocomplete="off" required />
<label for="lname" class="label-name">
<span class="content-name" name="lname">{{dbEntry.suspect_last_name}}</span>
</label></div>
<div class="forminputs">
<input type="text" id="fatherName" name="fatherName" autocomplete="off" required />
<label for="fatherName" class="label-name">
<span class="content-name" name="fatherName">{{dbEntry.suspect_father_name}}</span>
</label></div>
<div class="forminputs">
<input type="text" id="motherName" name="motherName" autocomplete="off" required />
<label for="motherName" class="label-name">
<span class="content-name" name="motherName">{{dbEntry.suspect_mother_name}}</span>
</label></div>
<div class="home-Button">
<button id="save" name="save" type="submit">Edit</button>
</div>