简体   繁体   中英

Ajax query not working in python django?

I want to change the status of data coming from table but it seems like like i have messed up some code in it.

my ajax request:-

function changeStatusDataById(object) {
    var baseURL = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '');
    var r = confirm("Are You sure we want to change status ?");
    if (r == true) {
        var requestData = {};
        var action = object.getAttribute("action");
        var id = object.getAttribute("id");
        requestData.action = action;
        requestData.id = id;
        $.ajax({
            url: baseURL + 'promoted-user/list/changeStatus/',
            method: 'POST',
            dataType: "json",
            contentType: "application/json",
            data: JSON.stringify(requestData),
            beforeSend: function () {
                var text = 'changing status . please wait..';
                ajaxLoaderStart(text);

            },
            success: function (data) {
                ajaxLoaderStop();
                location.reload();
            },
            error: function (jqXHR, ex) {
                ajaxLoaderStop();
            }
        });
    }
    return false;
}

my django url:-

url(r'^promoted-user/list/changeStatus/$', delete.promoter_change_status, name='promoter-change-status')

my views:-

@login_required
@csrf_exempt
def promoter_change_status(request):
    response_data = dict()
    message = ''
    status = "ERROR"
    if request.method == "GET":
        message = "GET method is not allowed"
    if request.method == "DELETE":
        message = "Delete method is not allowed"
    if request.method == "POST":
        request_data = body_loader(request.body)
        print 'hello'
        try:
            action = request_data['action']
            id = request_data['id']
            if action is not None and id is not None and action != '' and id != '':
                status = "OK"
                message = "Status Changed successfully........."
                if action == "newsDelete":
                    object_data = News.objects.using("cms").get(id=id)
                    object_data.status = not object_data.status
                    object_data.save()
                    messages.success(request, 'Status Changed  successfully')
                else:
                    message = "action and id is required field................."
        except ObjectDoesNotExist:
            status = "ERROR"
            message = "id does not exist..........."
        except Exception as e:
            print e
            message = e.message + " is required field................."
    response_data['message'] = message
    response_data['status'] = status
    return HttpResponse(json.dumps(response_data))

calling ajax on td of table:-

<td class="text-center">
                                            <a href="#" class="fg_red changeStatusDataById" data-toggle="modal"
                                               action="{{ object_name }}" id="{{ item.newsId.id }}">
                                                <i class="fa fa-trash"></i>
                                            </a>
                                        </td>

but its not working. even the hello is not printed from my view

i was just missing / on the url when i was calling ajax.

  $.ajax({
            url: baseURL + '/promoted-user/list/changeStatus/',
            method: 'POST',
            dataType: "json",
            contentType: "application/json",
            data: JSON.stringify(requestData),
            beforeSend: function () {
                var text = 'changing status . please wait..';
                ajaxLoaderStart(text);

            },

rest of the code is fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM