accounts/views.py
def login_user(request):
    """Validate user credentials and log in else display error."""
    next_page = request.GET.get('next')
    if next_page:
        next_page = next_page.strip()

    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse('home_page'))

    if request.method == 'POST':
        form = UserLoginForm(data=request.POST)

        if form.is_valid():
            username = form.cleaned_data.get('username')
            user = authenticate(
                username=username,
                password=form.cleaned_data.get('password')
            )

            if user is not None:
                if user.is_active:
                    login(request, user)
                    messages.success(
                        request, 'Login successful. Welcome %s' % username)

                    if next_page:
                        return HttpResponseRedirect(next_page)
                    return HttpResponseRedirect(reverse('home_page'))
                else:
                    return render(request, 'accounts/check_email.html')

            else:
                messages.error(request, 'User does not exist.')

        else:
            messages.error(request, 'Invalid credentials')

    else:
        form = UserLoginForm()
    return render(request, 'accounts/login_user.html', {'form': form})
Validate user credentials and log in else display error.
Validate user credentials and log in else display error.
Validate user credentials and log in else display error .
def login_user(request):
    next_page = request.GET.get('next')
    if next_page:
        next_page = next_page.strip()
    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse('home_page'))
    if request.method == 'POST':
        form = UserLoginForm(data=request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            user = authenticate(
                username=username,
                password=form.cleaned_data.get('password')
            )
            if user is not None:
                if user.is_active:
                    login(request, user)
                    messages.success(
                        request, 'Login successful. Welcome %s' % username)
                    if next_page:
                        return HttpResponseRedirect(next_page)
                    return HttpResponseRedirect(reverse('home_page'))
                else:
                    return render(request, 'accounts/check_email.html')
            else:
                messages.error(request, 'User does not exist.')
        else:
            messages.error(request, 'Invalid credentials')
    else:
        form = UserLoginForm()
    return render(request, 'accounts/login_user.html', {'form': form})
def login_user ( request ) : next_page = request . GET . get ( 'next' ) if next_page : next_page = next_page . strip ( ) if request . user . is_authenticated ( ) : return HttpResponseRedirect ( reverse ( 'home_page' ) ) if request . method == 'POST' : form = UserLoginForm ( data = request . POST ) if form . is_valid ( ) : username = form . cleaned_data . get ( 'username' ) user = authenticate ( username = username , password = form . cleaned_data . get ( 'password' ) ) if user is not None : if user . is_active : login ( request , user ) messages . success ( request , 'Login successful. Welcome %s' % username ) if next_page : return HttpResponseRedirect ( next_page ) return HttpResponseRedirect ( reverse ( 'home_page' ) ) else : return render ( request , 'accounts/check_email.html' ) else : messages . error ( request , 'User does not exist.' ) else : messages . error ( request , 'Invalid credentials' ) else : form = UserLoginForm ( ) return render ( request , 'accounts/login_user.html' , { 'form' : form } )
Validate user credentials and log in else display error.
Validate user credentials and log in else display error .
"""Validate user credentials and log in else display error."""
request