如何在不更新表单中所有字段的情况下进行更新
Posted
技术标签:
【中文标题】如何在不更新表单中所有字段的情况下进行更新【英文标题】:how to make update without updating all fields in the form 【发布时间】:2019-12-01 03:52:50 【问题描述】:我有一个 django 网站,其中包含一个更新功能,该功能根据所选 ID 过滤数据并返回请求记录的所有相关值,其中允许用户编辑数据且字段不能为空。
只有在所有字段都被编辑后才完成更新
我的问题是即使只编辑了一个字段
,如何进行更新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>
【问题讨论】:
为什么不像form = YOURFORM(request.POST or None, instance=EXISTING_INSTANCE)
那样将您的实例传递给您的表单?你可以阅读here i.e.。
form = YOURFORM() 你的意思是django模型形式??我在设计这个表单时遇到了问题,否则最好使用模型表单来执行 CRUD 功能
【参考方案1】:
像这样在模板中提供输入值:
<div id='left-column-Input' class="formInput" include="select()">
<div class="forminputs">
<input type="text" id="fname" name="fname" value = "dbEntry.suspect_name" autocomplete="off" required />
<label for="fname" class="label-name">
<span class="content-name" name="fname">dbEntry.suspect_name</span>
</label></div>
【讨论】:
那不带值的内容呢?以上是关于如何在不更新表单中所有字段的情况下进行更新的主要内容,如果未能解决你的问题,请参考以下文章
Rails - 如何在不更改页面和更新视图的情况下提交表单?
如何在不订阅 Angular 表单提交的情况下创建/更新项目
在 FaunaDB 中更新记录时,如何在不传入每个字段和子对象的情况下更新一个字段?