wgw
wgw

Reputation: 601

Django + Angular: how to send angularjs $http data or params to django and interpret in django?

Why can't I use params or data with $http to communicate with Django?

I have an angularjs page that gets data from a django view. In one case, I have a django view called /display that grabs a file off the server and displays it as plain text.

this.loadfile = function (clickedItem) {
    $http({method: 'GET' , url: '/display/'+ clickedItem.fileName})
        .success(function(data) {
                $scope.fileView.text = data;

        });
};

The call to /display/myfile.txt works nicely, since the filename is a simple string, as is the returned data.

Now I need to send a more complex, multiline parameter to django: a list of searchStrings and a list of filenames:

  this.concord = function (searchStrings, filenames) {

        $http({method: 'GET' ,
                url: '/concord/',
                data: {"searchStrings":searchStrings, "filenames":filenames}
            )
            .success(function(data) {
                console.log(data); // log the data coming back.
                $scope.concord = data;
            });
    };

According to AngularJS - Any way for $http.post to send request parameters instead of JSON?, that should work with

url(r'^concord/(?P<jsondata>.*)$', views.concord, name="concord"),

where my view is:

def concord(request,jsondata) :
    jObj=simplejson.loads(jsondata)
    return HttpResponse(jObj["files"])

(I tried parsing request.body as well; was empty)

On the other hand, if I explicitly attach it to the url, I do get something back:

 $http({method: 'GET',
                url: '/concord/'+JSON.stringify({"searchStrings":searchStrings, "filenames":filenames}),

            }
            )
            .success(function(data) {
                console.log(data);
                $scope.concordResults = data;
            });

That works!

But I must be doing this in worst way possible. Is there a way to use params or data to communicate with Django?

(PS: I have avoided django-angular package because I am already overwhelmed with learning packages: django rest, angular, bootstrap, etc... I really just want to start with bare-bones communication between angular and django. Later I will dive into more packages.)

Upvotes: 3

Views: 2903

Answers (1)

Luke Skywalker
Luke Skywalker

Reputation: 1521

If you look at angularjs docs (https://docs.angularjs.org/api/ng/service/$http) you see that you can get params and post data like json in your case.

When you post, you'll do something like this:

$http.post('http://www.example.com/post_data_url',
           { 'key': $scope.value }
          );

On django side, don't add any parameter to the URL, the JSON will be inside the request, in the body to be more precise:

import json

def concord(request):
  dictionary = json.loads(request.body)
  # to use the value, access the created dictionary
  print dictionary['key']
  [...]

Upvotes: 3

Related Questions