Skip to content

Instantly share code, notes, and snippets.

@nacady
Created October 13, 2014 09:44
Show Gist options
  • Save nacady/0b029993b579e2cbfc98 to your computer and use it in GitHub Desktop.
Save nacady/0b029993b579e2cbfc98 to your computer and use it in GitHub Desktop.
ajax login with django-allauth and django-angular ref: http://qiita.com/nacady/items/389305cc249c5744402c
angular.module('app', ['ng.django.forms']).config(function($httpProvider) {
$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
$httpProvider.defaults.headers.common['X-CSRFToken'] = '{{ csrf_token }}';
}).controller('ctrl', function($scope, $http, $window, djangoForm) {
$scope.submit = function($event) {
if($scope.form){
$http({
method: 'POST',
url: $scope.action,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: $scope.form
}).success(function(d) {
//$window.location.href = d.location;
console.log(d);
}).error(function(d) {
if(typeof d == 'object' && 'form_errors' in d){
console.log(d);
djangoForm.setErrors($scope.login_form, d.form_errors);
}
});
}
};
});
from allauth.account import forms
from djangular.forms import NgFormValidationMixin, NgModelFormMixin
from djangular.styling.bootstrap3.forms import Bootstrap3FormMixin
class LoginForm(NgModelFormMixin, NgFormValidationMixin, Bootstrap3FormMixin, forms.LoginForm):
form_name = 'login_form'
scope_prefix='form'
<form name="{{ form.form_name }}" class="login" method="POST" ng-init="action = '{% url 'account_login' %}'" novalidate ng-controller="ctrl" ng-submit="submit()">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment