def login_view(request):
    response_data = {}
    # if request.method == 'GET':
    if request.method == 'POST':
        login_form = LoginForm(request.POST)
        if login_form.is_valid():
            #validation successful
            username = login_form.cleaned_data['username']
            password = login_form.cleaned_data['password']
            #read data from db
            user = UserModel.objects.filter(username=username).first()
            if user:
                #compare password
                if check_password(password, user.password):
                    token = SessionToken(user=user)       #if password matches session token generaed
                    token.create_token()
                    token.save()
                    response = redirect('/feed/')
                    response.set_cookie(key='session_token', value=token.session_token)
                    return response
                else:
                    return render(request,'login_fail.html')
            else:
                return render(request, 'login_fail.html')
        else:
            return HttpResponse("Invalid form data.")
    elif request.method == 'GET':
        form = LoginForm()
        response_data['form'] = form

    return render(request, 'login.html', response_data)
Exemple #2
0
def login_view(request):
    response_data = {}
    if request.method == 'GET':
        #to do: display login form
        form = LoginForm()
    elif request.method == 'POST':
        #to do: process form data
        form = LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            #check user exist in db or not
            user = UserModel.objects.filter(username=username).first()
            if user:
                #compare password
                if check_password(password, user.password):
                    #login successful
                    new_token = SessionToken(user=user)
                    new_token.create_token()
                    new_token.save()
                    response = redirect('/feed/')
                    response.set_cookie(key='session_token',
                                        value=new_token.session_token)
                    return response
                else:
                    #password incorrect.
                    HttpResponse("wrong password entered")
                    response_data[
                        'message'] = 'Incorrect Password! Please try again!'
            else:
                HttpResponse("form data is not valid")

    response_data['form'] = form
    return render(request, 'login.html', response_data)
Exemple #3
0
def login_view(request):
    response_data = {}
    if request.method == "POST":
        form = LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            user = UserModel.objects.filter(username=username).first()

            if user:
                if check_password(password, user.password):
                    token = SessionToken(user=user)
                    token.create_token()
                    token.save()
                    response = redirect('/feed/')
                    response.set_cookie(key='session_token', value=token.session_token)
                    return response
                else:
                    response_data['message'] = 'Incorrect Password! Please try again!'
                   
    elif request.method == 'GET':
        form = LoginForm()

    response_data['form'] = form
    return render(request, 'login.html', response_data)
Exemple #4
0
def login_view(request):
    if request.method == 'GET':                                #display login form
        login_form = LoginForm()
        template_name = 'login.html'

    elif request.method == 'POST':                             # process the form data
        login_form = LoginForm(request.POST)
        if login_form.is_valid():                              # validation sucessful
            username = login_form.cleaned_data['username']
            password = login_form.cleaned_data['password']     # read data from db
            user = UserModel.objects.filter(username=username).first()
            if user:
                if check_password(password,user.password):     # compare the password
                    #login sucessful
                    token = SessionToken(user=user)
                    token.create_token()                        #calling to create_token()
                    token.save()
                    response = redirect('/feed')
                    response.set_cookie(key='session_token',value=token.session_token)
                    return response
                else:
                    #login failed
                    template_name='login_fail.html'
            else:
                #user does not exist in db
                template_name = 'login_fail.html'
        else:
            # validation failed
            template_name='login_fail.html'
    return render(request,template_name,{'login_form':login_form})
Exemple #5
0
def login_view(request):
    #----------------------------------here is the function logic-----------------------------------------------------------------
    if request.method == 'GET':
        #Display Login Page
        login_form = LoginForm()
        template_name = 'login.html'
    #---------------------------------------Elif part---------------------------------------------------------------------------------
    elif request.method == 'POST':
        #Process The Data
        login_form = LoginForm(request.POST)
        if login_form.is_valid():
            #Validation Success
            Username = login_form.cleaned_data['Username']
            Password = login_form.cleaned_data['Password']
            #read Data From db
            user = UserModel.objects.filter(Username=Username).first()
            if user:
                #compare Password
                if check_password(Password, user.Password):
                    token = SessionToken(user = user)
                    token.create_token()
                    token.save()
                    response = redirect('feed/')
                    response.set_cookie(key='session_token', value=token.session_token)
                    return response
                    #successfully Login

                    template_name = 'login_success.html'
                else:

                    #Failed
                    template_name = 'login_fail.html'
            else:
                #user doesn't exist
                template_name = 'login_fail.html'
        else:
            #Validation Failed
            template_name = 'login_fail.html'


    return render(request,template_name,{'login_form':login_form})