Tell me how to make a POST request to the api. Using restangular


I’m using Django REST framework.
Writing API works if you manually enter data on a page: (API)

class AddComment(generics.CreateAPIView):
Create new comment.
Example API structure:
    "post_pk": 1,
    "name": "MyName",
    "email": "",
    "text": "This is text message!"
model = Comment
serializer_class = CommentSerializers

And api works.


var app = angular.module('controllers', []);

app.controller('CommentAddCtrl', function($scope, Restangular){
    $scope.logComment = function(mail, name, text){
        var url = window.location;
        var pk = url.hash.split('$pk:')[1];
        var url_add_comment_api = './en/api/v1/add_comment';

        if (mail === undefined || name == undefined || text === undefined)
            alert ('Error!');
        else {
            var comment = {
                  post_pk: pk,
                  name: name,
                  email: mail,
                text: text
            this.add_comment = Restangular.all(url_add_comment_api).post(comment);

When the console getting error:


You may be missing a CSRF token somewhere in there. See more here: Django Cross Site Request Forgery Protection

Answered By – Justin Andrew

Answer Checked By – Marie Seifert (AngularFixing Admin)

Leave a Reply

Your email address will not be published.