I have a login and sign_up view like this:
def sign_up(request):
form = UserCreationForm()
registered = False
if request.method == 'POST':
form = UserCreationForm(data=request.POST)
if form.is_valid():
form.save()
registered = True
context = {
'form':form,
'registered':registered,
}
return render(request, 'users/sign_up.html',context)
def login_page(request):
form = AuthenticationForm()
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username,password=password)
if user is not None:
login(request,user)
return HttpResponseRedirect(reverse('index'))
context = {
'form':form,
}
return render(request, 'users/login.html',context)
What I actually want is that if the user is not logged out first and tries to access the sign up or login page, they are redirected to the logout page to first logout. So, they can't access these 2 pages unless they are logged out. How can I do this?