Access角度如何在django视图中传递参数

时间:2017-04-18 15:28:24

标签: javascript angularjs django django-views

我试图在django中使用angular $ http.post方法发布数据,我想知道如何在我的django视图中访问该参数值: -

这是我的controller.js

    var nameSpace = angular.module("ajax", ['ngCookies']);

nameSpace.controller("MyFormCtrl", ['$scope', '$http', '$cookies',
function ($scope, $http, $cookies) {
    $http.defaults.headers.post['Content-Type'] = 'application/json';
    // To send the csrf code.
    $http.defaults.headers.post['X-CSRFToken'] = $cookies.get('csrftoken');

    // This function is called when the form is submitted.
    $scope.submit = function ($event) {
        // Prevent page reload.
        $event.preventDefault();
        // Send the data.
        var in_data = jQuery.param({'content': $scope.card.content,'csrfmiddlewaretoken': $cookies.csrftoken});
        $http.post('add_card/', in_data)
          .then(function(json) {
            // Reset the form in case of success.
            console.log(json.data);
            $scope.card = angular.copy({});
        });
    }
 }]);

以下是我试图访问我的视图功能中的内容: -

card.content =request.POST.get('content')

其中card是我模型的对象,但我得到了NameError:名称'content'未定义,请帮帮我!

1 个答案:

答案 0 :(得分:0)

你可以试试没有jquery Param。尝试传递$ http.post(url,data,config)

中的简单对象
    var nameSpace = angular.module("ajax", ['ngCookies']);

nameSpace.controller("MyFormCtrl", ['$scope', '$http', '$cookies',
function ($scope, $http, $cookies) {
    $http.defaults.headers.post['Content-Type'] = 'application/json';
    // To send the csrf code.
    $http.defaults.headers.post['X-CSRFToken'] = $cookies.get('csrftoken');

    // This function is called when the form is submitted.
    $scope.submit = function ($event) {
        // Prevent page reload.
        $event.preventDefault();
        // Send the data.
        var config = {
            headers : {
                'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
            }
        }
        var in_data = {content: $scope.card.content,csrfmiddlewaretoken: $cookies.csrftoken};
        $http.post('add_card/', in_data, config)
          .then(function(json) {
            // Reset the form in case of success.
            console.log(json.data);
            $scope.card = angular.copy({});
        });
    }
 }]);