通过ajax请求处理django表单时出错

时间:2015-07-24 14:52:19

标签: python ajax django

这是基于类视图的post方法:

def post(self, request, *args, **kwargs):
        if 'delete' in request.POST:
            owned_license_id = request.POST.get('id', '')
            if owned_license_id:
                try:
                    OwnedLicense.objects.get(id=owned_license_id).delete()
                    return HttpResponse("success")
                except:
                    return HttpResponse("failure")
        elif 'add' in request.POST:
            form = OwnedLicenseForm(request.POST or None)
            if form.is_valid():
                form.save()
            else:
                errors = form.errors
                return HttpResponse(json.dumps(errors))

和我在jtml文件中的javascript脚本函数:

function add_new_license(btn, id)
    {
       alert($("#add_license_form").serialize())
       $.ajax({
            url: "/manage/licenses/{{mls_signup_code}}/{{agent_id}}/" + id + "/",
            type: "post",
            data: $("#add_license_form").serialize(),
            success: function(response) {
            console.log(response)
            }
        })

    }

add_license_form是我的表单ID。现在在我看来,每当我尝试form.is_valid()时,它始终会生成False并且form.error中没有错误。

我在这里错过了一些东西吗?

这是我的要求。邮件:

<QueryDict: {'add': ['add'], 'charge_amount': ['0.02'], 'is_suspended': ['on'], 'start_date': ['07/21/2015'], 'tier_type': ['5'], 'auto_renew': ['on'], 'deactivation_time': [''], 'product_license': ['2'], 'is_trial': ['on'], 'reactivation_time': [''], 'tier_id': ['1'], 'payment_day': ['1'], 'expiration_date': ['2015-08-24']}>

0 个答案:

没有答案