def login_view(request):
    if request.user.is_authenticated():
        return redirect('mainsite:main')    
    
    if request.method == 'POST':
        form = LoginForm(data=request.POST)
        # Checks if form is filled in correctly
        if form.is_valid():
            user = form.login(request)
            if user:
                login(request, user)
                # Updates lease status on login
                services.update_lease_status(user)
                return redirect('mainsite:main')
    else:
        form = LoginForm()
        
    return render(request, 'registration/login.html', {
        'form': form
    })
def logout_view(request):
    # Also updates lease status on logout
    services.update_lease_status(request.user)    
    logout(request)
    
    return redirect('landing')