I am getting the Forbidden (CSRF token missing or incorrect.) error when I try to use login page.
The scenario is as follows:
- An user has two tabs open.
- Both tabs are login pages.
- In tab 1, user successfully logged in, and was redirected to a new page where login is required.
- In tab 2, user hasn't refreshed the page, and is still in the login page. In the Django backend, user is already authenticated, but the front-end template hasn't noticed it yet.
- In tab 2, when I click on login button, I get
Forbidden (CSRF token missing or incorrect.)error. - I made sure that
csrf_tokenis in the form. - This error occurs only when I'm using two tabs.
- I'm using AJAX
Why is this happening? How can I fix it?
I don't know this would help, but here is my views.py for login
class Login_View(LoginView):
template_name = 'login.html'
def post(self, request, *args, **kwargs):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
response_data = {}
if user is not None:
if user.is_active:
login(request, user)
response_data['result'] = 'success'
else:
return HttpResponse("Inactive user.")
else:
response_data['result'] = 'fail'
return HttpResponse(json.dumps(response_data), content_type="application/json")