1

I'm using Angular $http to post data to django, but django isn't receiving it. I must be either misusing $http (because this worked with ajax) or my django view is wrong.

<div ng-controller="mycontroller2">
    <form ng-submit="submit()">
    {% csrf_token %}
       Search by name:<input ng-model="artiste" />
       <input type="submit" value="submit" />
    </form>
    <table>
        <tr ng-repeat="artist in artists">
            <td> {({ artist.fields.link })} </td>
        </tr>
    </table>
</div>
<script>
artApp.controller('mycontroller2', ['$scope', '$http',
function($scope, $http){
    $scope.submit = function(){           

    var postdata = {
        method: 'POST',
        url: '/rest/',
        data: {
            "artiste": $scope.artiste
        },
        headers: {
            'X-CSRFTOKEN': "{{ csrf_token }}"
        }
    };
    $http(postdata)
        .success(function(data){
            $scope.artists = data;
        })
    }
}]);
</script>

The request handler in views.py looks like

def rest(request):

    artistname = request.POST.get("artiste") # should get 'da vinci'
    response_data = {}
    response_data = serializers.serialize("json", Art.objects.filter(artist__contains=artistname))
    return HttpResponse(json.dumps(response_data), content_type="application/json")

The error I'm getting from Django is ValueError at /rest/ Cannot use None as a query value.

My call to get the value of "artiste" must not be returning 'da vinci' from the $http data object. I'm sure it's sent successfully because the data artiste: da vinci is shown in my headers in devtools. Django just isn't getting that value. Something wrong with the request.POST.get("artiste") call?

4
  • 1
    have you checked (the "Network" tab on your console) if the data (artiste) is really sent to the server? Commented Dec 4, 2014 at 10:21
  • Yes I believe so. In the Network tab it shows the 500 error, I click on that and go to "Headers" and under "Request Payload" it shows artiste: "da vinci". Is that what you are talking about? Commented Dec 4, 2014 at 11:19
  • 1
    yes.. so it's really a django problem. You'll need to inpect the request object using pdb or runserver_plus Commented Dec 4, 2014 at 11:27
  • 1
    I solved it. Since the data is raw json data, I had to get it by doing json.loads(request.body) rather than request.POST.get(). Shoutout to #django IRC room. Commented Dec 4, 2014 at 11:43

1 Answer 1

4

Since the data from my $http request is raw json data, my Django request handler had to be changed to deal with that. Now the function (in views.py) looks like

def rest(request):

    artistname = json.loads(request.body)      # <- accepts raw json data
    artistname = artistname['artiste']         # <- and now I can pull a value
    response_data = {}
    response_data = serializers.serialize(
                    "json", Art.objects.filter(artist__contains=artistname))

    return HttpResponse(json.dumps(response_data), content_type="application/json")
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.