0

I have been having great difficulties understanding how to log into my own website. I have users registered in my database, I have created another ModelForm aside from the registration form. I just do not understand how the login view will check to see if the (email) & (password) is in the database and actually log the user in the main website area. Could someone give me a better explanation on how to check the database to see if the information is correct and log into your own website?

NOTE: I am not looking for anything to do with admin.py or the admin page, this is what I seem to find a lot of information on.

**********************************VIEWS.PY***********************************************
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect
from django.views.generic.base import TemplateView
from django.views.generic.edit import FormView
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.conf import settings
from App1.models import RegisterForm, SignInForm, ModelRegister
# Create your views here.


class LoginView(FormView):

    template_name = 'App1//login.html'
    form_class_signin = SignInForm()
    success_url = '/Main-name/'
    login_url = '/login/'

    def get(self, request):
        form_class_signin = SignInForm()
        return render(request, 'App1/login.html', {'form_class_signin': form_class_signin})

    def post(self, request):
        form_class_signin = SignInForm()
        return render(request, 'App1/login.html', {'form_class_signin': form_class_signin})

    def user_log_in(self, request):
        success_url = '/Main-name/'
        if request.method == 'POST':
            emaillog = request.POST['email']
            passlog = request.POST['password']

            user = authenticate(request, username=emaillog, password=passlog)

            if user is not None:
                if request.user.is_authenticated():
                    login(self.request, user)
                    return HttpResponseRedirect(success_url)
                if not request.user.is_authenticated:
                    return HttpResponseRedirect(success_url)
                else:
                    return HttpResponse("ACCOUNT NOT ACTIVE")
            else:
                print("Someone tried to login and fialed!")
                print("Username: {} and password {}".format(emaillog, passlog))
                return HttpResponse("Invalid login details")
        else:
            return render(request, 'App1/login.html', {'form_class_signin': form_class_signin})

        @login_required(login_url='/login/')
        def user_logout(self, request):
            success_url = '/login/'
            logout(request)
            return redirect(success_url)

        @login_required(login_url='/login/')
        def special(self, request):
            return HttpResponse("Welcome, User Active & Authenticated!")
        return render(request, 'App1/login.html', {'form_class_signin': form_class_signin})

*****************************************END VIEWS.PY**************************************
dmitryro
  • 3,463
  • 2
  • 20
  • 28
  • 1
    If all you want to do is to change the styling of the login form, you don't have to implement login yourself. You can use the [built-in authentication views](https://docs.djangoproject.com/en/2.0/topics/auth/default/#all-authentication-views) and simply use your own template. – Selcuk Jul 03 '18 at 00:39
  • I did try that but the only problem I am having is that it takes away my form for that template. –  Jul 03 '18 at 00:55
  • 2
    I recommend using django's built in authentication system. Check out my answer here [link](https://stackoverflow.com/a/51146142/9913620) – icyfire Jul 03 '18 at 03:28
  • Thanks for writing in you have been very helpful :) I just have one question about your answer there. I had notice you created a html form instead of a ModelForm or Form. Is that what you would suggest because I had created a ModelForm for the User login? –  Jul 03 '18 at 03:43

0 Answers0