I have a django application that is successfully able to signup and login a user.However I am unable to logout a user.
In the front end, I have a webpage that contains a power button icon, which on clicking should trigger a logout request.
I am using angular js for front end
index.html
<div class="col-xs-2">
<span style="opacity: 0.5;font-family: FontAwesome;font-size: 14px;color:#838F98;text-align:center;cursor:pointer" ng-click="logout()">
<i class="fa fa-power-off" aria-hidden="true"></i>
</span>
</div>
Here I use ngclick
to call the logout()
function that is defined in my index.js
index.js
$scope.logout = function() {
var url = '/logout';
var toSend = {
csrfmiddlewaretoken: '{{ csrf_token }}'
}
$http({
method: 'POST',
url: url,
data: toSend,
}).then(function(response) {
response.data;
})
};
This function calls the /logout
url for which I have defined an auth views
in urls.py
urls.py
from django.contrib.auth.views import login, logout
url(r'^logout$', logout, {'template_name': 'login.html'}),
But when I click the power icon on the webpage, I get a 403 Forbidden
error.It says CSRF verification failed. Request aborted
.But I am passing the csrf token in the javascript POST
call.
What am I doing wrong?