Ejemplo n.º 1
0
def user_login(request):
    if request.method == 'POST':
        login_form = UserLoginForm(request.POST)
        if login_form.is_valid():
            user = authenticate(username=login_form.cleaned_data.get('username'), 
                password=login_form.cleaned_data.get('password'))
            if user is not None:
                login(request, user)

                redirect_url = reverse_lazy('profile')
                return HttpResponseRedirect(redirect_url)

            login_form.add_error(None, 'check username and password')
    else:
        login_form = UserLoginForm()

    return render(request, 'login.html', {'login_form': login_form})
Ejemplo n.º 2
0
def login_view(request):
    if request.method == 'POST':
        form = UserLoginForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            user = authenticate(request=request, username=data['username'], password=data['password'])
            if user is not None:
                login(request, user=user)
                return HttpResponseRedirect(reverse_lazy('two-factor-verification', kwargs={'source': 'login'}))
            else:
                # return HttpResponse('Wrong credentials')
                form.add_error(None, "Wrong Credentials")
                context = {'form': form}
                return render(request, 'user/login.html', context=context)
        else:
            context = {'form': form}
            return render(request, 'user/login.html', context=context)
    else:
        form = UserLoginForm()
        context = {'form': form}
        return render(request, 'user/login.html', context=context)
Ejemplo n.º 3
0
def login(request):
    if request.method == 'POST':
        form = UserLoginForm(request.POST)
        if form.is_valid():
            user = auth.authenticate(
                username=request.POST.get('username_or_email'),
                password=request.POST.get('password'))

            if user is not None:
                auth.login(request, user)
                messages.error(request, "You have successfully logged in")

                if request.GET and 'next' in request.GET:
                    next = request.GET['next']
                    return HttpResponseRedirect(next)
                else:
                    return redirect(reverse('profile'))
            else:
                form.add_error(None,
                               "Your username or password was not recognised")

    else:
        # this handles the initial get request
        # return a blank form for user to login
        form = UserLoginForm()

    # prepare args to pass to render function
    #   form:   this is the form which will be rendered to html (UserLoginForm)
    #   next:   if the url of the request included a next query string '?next=' pass it to the form
    #           so that it can be included in the url when the form is resubmitted
    #           see handling of post method: next = request.GET['next']
    args = {
        'form':
        form,
        'next':
        request.GET['next'] if request.GET and 'next' in request.GET else ''
    }
    args.update(csrf(request))
    return render(request, 'login.html', args)