403.在使用angular.js post和django rest框架时出错

时间:2015-10-12 16:55:43

标签: angularjs django rest django-rest-framework http-status-code-403

我尝试使用前面的angular.js和后端的api休息来创建一个非常简单的登录表单。这是html方面的表格:

<form action="" method="post">
    {% csrf_token %}
    <div class="modal-body">
        <div class="form-group">
            <label for="login-username" class="control-label">Email:</label>
            <input type="text" class="form-control" name="username" id="login-username" ng-model="loginData.email" required>
        </div>
        <div class="form-group">
            <label for="login-password" class="control-label">Password:</label>
            <input type="text" class="form-control" name="password" id="login-password" ng-model="loginData.password" required>
        </div>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary" ng-click="login()">Login as {$ loginData.email $}</button>
    </div>
</form>

这是我的角度控制器(注意:我知道这里没有问题,因为我有一个非常类似的寄存器功能,工作得很好):

mainApp.controller("mainCtrl", function($scope, $http) {
    $scope.loginData = {"email":"bernard@mail.com","password":"pass"};
    $scope.login = function() {
        return $http.post('/api/v1/auth/login/', 
            $scope.loginData
        ).success(function(data, status){
            alert("success : "+status);
            console.log(data);
        }).error(function(data, status){
            alert("error : "+status);
        });
    }
});

这是我的角度应用程序,其中包含csrf标题:

var mainApp = angular.module("mainApp", []).config(function($interpolateProvider) {
    $interpolateProvider.startSymbol('{$');
    $interpolateProvider.endSymbol('$}');
}).run(run);


function run($http) {
    $http.defaults.xsrfHeaderName = 'X-CSRFToken';
    $http.defaults.xsrfCookieName = 'csrftoken';
}

在django方面,我有一个登录视图,完全适用于get而不是post方法。那就是我迷失的地方。

class LoginView(views.APIView):
    """
    View for login user
    """
    queryset = BaseUser.objects.all()

    def get(self, request, format=None):
        print("get_login098765") # THIS IS PRINTED PERFECTLY
        return Response()

    def post(self, request, format=None): #THIS THROWS 403 ERROR
        print("post_login098765") #THIS IS NOT PRINTED
        return Response()

我错过了什么?谢谢!

编辑:

我收到一个名为login的文件,其中包含:{"detail":"Authentication credentials were not provided."}

2 个答案:

答案 0 :(得分:2)

根据我的阅读,需要在配置函数中应用CRSF参数:

var mainApp = angular.module("mainApp", []).config(function($interpolateProvider, $httpProvider) {
    $interpolateProvider.startSymbol('{$');
    $interpolateProvider.endSymbol('$}');
    $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
    $httpProvider.defaults.xsrfCookieName = 'csrftoken';
}).run(run);

答案 1 :(得分:1)

响应您的编辑,消息: Django REST Framework正在抛出library("ggplot2") ggplot(dAll,aes(year,count))+geom_point()+facet_wrap(~frame) ,它与您对该ViewSet的权限有关。我建议您查看http://www.django-rest-framework.org/api-guide/authentication/

这可能包括将默认身份验证类添加到您的设置文件中,如下所示:

    Scheduler.get().scheduleDeferred(new ScheduledCommand() {

        @Override
        public void execute() {
            // update your cells
        }
    });

或基于视图,如下:

{"detail":"Authentication credentials were not provided."}

更新:由于使用REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.BasicAuthentication', 'rest_framework.authentication.SessionAuthentication', ) } 作为权限工作,您可以执行以下操作将这些权限限制为后期路由。这是一个例子:

from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.permissions import IsAuthenticated

class LoginView(api.APIView):
  authentication_classes = (SessionAuthentication, BasicAuthentication)
  permission_classes = (IsAuthenticated,)
  [...]

这将覆盖此端点上POST请求的权限,默认为其他路由的IsAuthenticated。