0

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/";   
     }
    });
 });

视图.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 中发布绑定值。需要帮助来解决这个问题。

更新:

raise Exception(followup_form.errors)在 form.is_valid() 之后的 views.py 中,我在收到此错误的帮助下检查了我的表单是否有错误,这"No exception supplied"是什么意思。

谢谢

4

0 回答 0