的javascript:
$('.update').click(function () {
var id = $(this).attr('id');
var firstname = $('#id_firstname').val();
var lastname = $('#id_lastname').val();
var email = $('#id_email').val();
var phonemobile = $('#id_phone_mobile').val();
var csrf_token = $("#csrf_token").val();
$.ajax({
data:{
csrfmiddlewaretoken: ('{{csrf_token}}'),
id:id,
firstname:firstname,
lastname:lastname,
email:email,
phone_mobile:phonemobile,
update:true
},
type:'POST',
url: '/setting/edit-follower/',
success: function() {
window.location.href = "/setting/follow-up/";
}
});
});
views.py
def edit_follower(request):
followup = FollowupUser.objects.get(id=request.POST['id'])
followup_form = FollowupSettingsForm()
if request.method == 'POST':
if 'edit_followup' in request.POST:
followup_form = FollowupSettingsForm(instance =followup)
if 'update' in request.POST:
followup_form = FollowupSettingsForm(request.POST,instance =followup)
if followup_form.is_valid():
followup_form.save()
return render(request,'edit_follower.html'{'form':followup_form,'followup':followup})
html(由django生成):
<div id="add_form" style="display:none" class="edit_follow">
<div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='c5c613de4180a9f33dca00d336cada8a' /></div>
<h2> Follow-up details</h2>
<br />
<table width="100%">
<tr>
<td style="width:100px;">First name:</td><td><input id="id_firstname" type="text" name="firstname" maxlength="100" /></td>
</tr>
<tr>
<td style="width:100px;">Last name:</td><td><input id="id_lastname" type="text" name="lastname" maxlength="100" /></td>
</tr>
<tr>
<td>Email:</td><td><input id="id_email" type="text" name="email" maxlength="100" /></td>
</tr>
<tr>
<td>Daytime phone:</td><td><input id="id_phone_mobile" type="text" name="phone_mobile" maxlength="100" /></td>
</tr>
<tr>
<td>Mobile phone:</td><td><input id="id_phone_daytime" type="text" name="phone_daytime" maxlength="100" /></td>
</tr>
<tr style="display:none;color:red" id="warning"><td colspan="2" align="center" >All fields are manditory</td></tr>
</table>
<div style="width:180px;margin:20px 5px 0 10px" align="right"><button style="margin-right:10px;" type="button" class="close" name="cancel" class="forward backicon">Cancel</button> <button type="button" id="{{ followup.id }}" name="update" title="Add" class="update">Add</button></div>
</div>
上面的views.py和javascript是通过popup更新数据行。如果表中只有一行数据,则更新正在工作,如果添加了超过1行的数据,我无法更新数据,无论我更改弹出框中的值都没有得到保存。我也没有收到任何错误。点击update
按钮,我在ajax中发布绑定值。需要帮助来解决这个问题。< / p>
更新
在form.is_valid()之后的views.py中,我在raise Exception(followup_form.errors)
的帮助下检查了我的表单是否有错误我收到此错误"No exception supplied"
这是什么意思。
由于