如何在django中将data-pk值从editable发送到服务器

时间:2016-05-20 06:35:04

标签: javascript jquery python django

如何将data-pk从editable发送到服务器。我已经使用过params和source但是它不起作用。请帮我解决这个问题。我认为$('#customer_name').attr('data-pk')pk中不起作用,但当我提醒它有价值时,我不明白

$('#customer_name').editable({
            type: 'text',
            pk: $('#customer_name').attr('data-pk'),
            url: '{% url 'change_customer_inline' %}',
            title: 'Enter customer name',
            success: function (response, newValue) {

                if (!response) {
                    return "Unknown error!"
                }
                if (response.success === false) {
                    return respond.msg;
                }

                $.ajax({
                    method: "POST",
                    url: '{% url 'change_customer_inline' %}',
                    dataType: 'JSON',
                    data: {
                        'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val(),
                        'customer_id': $('#customer_name').attr('data-pk'),
                    },
                    responseTime: 200,
                    response: function (settings) {
                        if (settings.data.value) {
                            this.responseText = '{"success": true}';
                        } else {
                            this.responseText = '{"success": false, "msg": "required"}';
                        }
                    }
                });
            }
        });

view.py

def change_customer_inline(request):
if request.method == 'POST':
    try:
        if request.is_ajax():
            customer_id = request.POST.get('customer_id')
            customer = Customer.objects.get(pk=customer_id)
            if request.POST.get('customer_name'):
                customer.name = request.POST.get('customer_name')
                customer.save()
    except OSError as e:
        error = messages.add_message(request, messages.ERROR, e, extra_tags='change_customer_inline')
        HttpResponse(error)
return render_to_response('order_list.html')

0 个答案:

没有答案