Hi Guys I'm trying to Submit the data's using angularjs in Django by refering this post, But Its not working in my case,
views.py
def add(request, template_name="form.html"):
if request.method == "POST":
print request.POST.get('fname')
return TemplateResponse(request, template_name)
form.html
<div ng-app="myApp">
<form action="." method="post" ng-controller="MyFormCtrl">{% csrf_token %}
<input type="text" name="fname" id="fname" ng-model="userprofile.fname" placeholder="First Name">
<input type="text" name="mname" id="mname" ng-model="userprofile.mname" placeholder="Middle Name">
<input type="text" name="lname" id="lname" ng-model="userprofile.lname" placeholder="Last Name">
<input type="text" name="email" id="email" ng-model="userprofile.email" placeholder="Email">
<input type="text" name="phone" id="phone" ng-model="userprofile.phone" placeholder="Phone Number">
<button ng-click="submit($event)">Save</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script type="text/javascript" src="https://raw.githubusercontent.com/angular/bower-angular-cookies/master/angular-cookies.js"></script>
<script type="text/javascript">
var nameSpace = angular.module("myApp", ['ngCookies']);
nameSpace.controller("MyFormCtrl", ['$scope', '$http', '$cookies',
function ($scope, $http, $cookies) {
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
$http.defaults.headers.post['X-CSRFToken'] = $cookies.csrftoken;
$scope.submit = function ($event) {
$event.preventDefault();
var in_data = jQuery.param({'fname': $scope.userprofile.fname,'csrfmiddlewaretoken': $cookies.csrftoken});
$http.post("{% url 'add_angularjs' %}", in_data)
.success(function(out_data) {
$scope.card = angular.copy({});
});
}
}]);
</script>
This is my code I Don't know what i Missed here, While I submitting the form its not triggering any thing.Please suggest me if I left any thing here and It will be greatfull for your suggestion. Thanks in advance.