4

I am trying to send my JavaScript this.click variable from my .js file to my Django view class Click using Ajax and getting the following error message...

AttributeError: 'WSGIRequest' object has no attribute 'data'

Here is my view...

class Clicks(View):
    def post(self, request):
        clicks = request.data.get('clicks')
        return JsonResponse({'status': True})

Here is my .js files ajax...

var image_movement = function(){  //Handles movement of tug of war image
this.total = 18
this.clicks = 0
this.move = function(num){  //Adds or subtracts one from total depending on which player calls it
    if(this.total == 0){
        $('#onewon').show();
        $.ajax({
            headers: {
                'Content-Type':'application/json',
                'X-CSRFToken': getCookie('csrftoken')
            },
            url:  'click',
            type: "POST",
            data: {clicks: this.clicks},
            success:function(response){
                alert("success: " + response);
            },
            error:function (xhr, textStatus, thrownError){
                alert("failure: " + xhr.statusText);
            }
        }).done(function (response) {
            alert("end");
        });

Here is my .js files function for obtaining cookie to give to ajax header...

function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
var csrftoken = getCookie('csrftoken');
}

Here is my url...

    url(r'^click$', Clicks.as_view()),

Things that I have tried...

1) Using clicks = request.DATA.get('clicks') instead of clicks = request.data.get('clicks') (get the same error as above)

2) Switching to own specific class based view to avoid any interference.

3
  • Hi ea87, Could it be that you access the data like this: request.POST['clicks'] Commented Dec 19, 2015 at 23:11
  • @adamteale Hey, that didn't work either. :/ Commented Dec 19, 2015 at 23:25
  • I'm not sure if this is the correct terminology but is you Ajax call making it to your Clicks view? Can you print something to test if it gets called? Commented Dec 19, 2015 at 23:28

1 Answer 1

2

I can't find anything in the Django Request object docs about request.data. Is it possible that you mean request.body?

On the other hand, Django Rest Framework does add a data attribute to the request object, but if that's the case - if your using DRF - you should make sure to import View from DRF and not from django.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.