I use django + angularjs. Incorrect data is sent and the form is not validated. ViewView based CreateView
. I redefined the form_valid
and form_invalid
to understand the reason and simply print self.request.POST
in them.
How to send a form via ajax
so that django can read them correctly?
now the data comes in the form
<QueryDict: {u'{"email":"blabla@gmail.com","number":456,"name":"Austria","text":"sdgdfhyju"}': [u'']}>
and should
<QueryDict: {u'text': [u'qwerty'], u'email': [u'some@site.com'], u'name': [u'Austria'], u'number': [u'9999']}>
code snippets:
<div style="width: 500px; margin: 0 auto"> <form name="userForm" ng-submit="processForm()"> <p> <label for="id_email">Email:</label> <input id="id_email" maxlength="254" name="email" type="email" ng-model="user.email"> <span ng-show="errorEmail">{{errorEmail}}</span> </p> <p> <label for="id_number">Number:</label> <input id="id_number" name="number" type="number" ng-model="user.number"> <span ng-show="errorNumber">{{errorNumber}}</span> </p> <p> <label for="id_name">Name:</label> <input id="id_name" maxlength="120" name="name" type="text" ng-model="user.name"> <span ng-show="errorName">{{errorName}}</span> </p> <p> <label for="id_text">Text:</label> <textarea cols="40" id="id_text" maxlength="1000" name="text" rows="10" ng-model="user.text"></textarea> <span ng-show="errorText">{{errorText}}</span> </p> <input type="submit" value="submit"> </form> </div> // angularjs $scope.user = {}; $scope.processForm = function() { $http({ method : 'post', url : '/some/', data : $scope.user, // в jquery всегда делал $(this).serialize() и проблем не было headers : {'Content-Type': 'application/x-www-form-urlencoded'} }) .success(function(data) { console.log(data); if (data.errors) { console.log('errors'); $scope.errorEmail = data.errors.email; $scope.errorNumber = data.errors.number; $scope.errorName = data.errors.name; $scope.errorText = data.errors.text; } else { console.log('blablabla'); } }); }; }]);
headers
and send as json - Grundy