2

I am having trouble with my http POST request in AngularJS. The error I am getting is:

POST http://localhost:8080/ 403 (Forbidden). 'rating-add' is a named url

So I don't know why it doesn't allow this. This is the template:

<script>
  var app = angular.module('myApp', []);
  app.controller('myCtrl', function($scope, $http){
    $scope.ratings = [];
    var data = $scope.myForm = {beer_name:'', score:'', notes:'', brewer:''};
    $scope.buttonClick = false;
    $scope.is_clicked = function() {
      $scope.buttonClick=true;
      console.log($scope.buttonClick)
    }
    $scope.submit_to_form = function() {
        $http({
          method: 'POST'
          url: 'rating-add'
          data: data
        });
    }
  })
  </script>

And the urls.py:

from django.conf.urls import url
from django.contrib import admin

from ratings.views import home, RatingCreate, delete, edit

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', RatingCreate.as_view(), name='rating-home'),
    url(r'rating/add/$', RatingCreate.as_view(), name='rating-add'),
    url(r'rating/delete/(?P<row_id>[0-9]+)/$', delete , name='rating-delete'),
    url(r'rating/edit/(?P<row_id>[0-9]+)/$', edit , name='rating-edit'),
]

1 Answer 1

2

You need to append a leading slash like this -

url : '/rating/add'

Check out the docs here.

You should print out your routes from Django to find out where to post.

Sign up to request clarification or add additional context in comments.

4 Comments

That doesn't work because 'rating-add' is a named url. So /rating-add doesn't match any urlconf. But even when I try '/rating/add/' I still get the same error.
Have you tried removing the trailing slash? Or try something like rating/add/1
Thanks for your help. How do I print my routes?
check this out github.com/django-extensions/django-extensions You can use it like this - $ ./manage.py show_urls

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.