From 7680e689129ea10ef7582de2d9f39c873c8f32ca Mon Sep 17 00:00:00 2001 From: joisemp Date: Mon, 1 Apr 2024 23:03:05 +0530 Subject: [PATCH] Implemented login page features along wit simple template --- core/forms.py | 12 ++++++++++++ core/templates/core/login.html | 6 ++++++ core/urls.py | 8 ++++++++ core/views.py | 19 +++++++++++++++++-- main/settings/base.py | 3 +++ main/urls.py | 3 ++- templates/landing_page.html | 7 ++++++- 7 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 core/forms.py create mode 100644 core/templates/core/login.html create mode 100644 core/urls.py diff --git a/core/forms.py b/core/forms.py new file mode 100644 index 0000000..c471090 --- /dev/null +++ b/core/forms.py @@ -0,0 +1,12 @@ +from django import forms +from django.contrib.auth import get_user_model +from django.contrib.auth.forms import AuthenticationForm + +User = get_user_model() + +class CustomAuthenticationForm(AuthenticationForm): + username = forms.CharField( + widget=forms.TextInput(attrs={'required': True})) + password = forms.CharField( + widget=forms.PasswordInput(attrs={'required': True})) + remember_me = forms.BooleanField(required=False) \ No newline at end of file diff --git a/core/templates/core/login.html b/core/templates/core/login.html new file mode 100644 index 0000000..7192a82 --- /dev/null +++ b/core/templates/core/login.html @@ -0,0 +1,6 @@ +

Login

+
+ {% csrf_token %} + {{ form.as_p }} + +
diff --git a/core/urls.py b/core/urls.py new file mode 100644 index 0000000..56da8bc --- /dev/null +++ b/core/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from .import views + +app_name = 'core' + +urlpatterns = [ + path('login/', views.LoginView.as_view(), name='login'), +] \ No newline at end of file diff --git a/core/views.py b/core/views.py index 5ff4990..de18709 100644 --- a/core/views.py +++ b/core/views.py @@ -1,5 +1,20 @@ -from django.shortcuts import render +from . forms import CustomAuthenticationForm from django.views import generic +from django.contrib.auth import views +from django.contrib.auth import login +from django.shortcuts import redirect class LandingPageView(generic.TemplateView): - template_name = 'landing_page.html' \ No newline at end of file + template_name = 'landing_page.html' + + +class LoginView(views.LoginView): + form_class = CustomAuthenticationForm + template_name = 'core/login.html' + + def form_valid(self, form): + remember_me = form.cleaned_data['remember_me'] + login(self.request, form.get_user()) + if remember_me: + self.request.session.set_expiry(1209600) + return redirect('landing-page') diff --git a/main/settings/base.py b/main/settings/base.py index f18a4f0..0805df3 100644 --- a/main/settings/base.py +++ b/main/settings/base.py @@ -81,3 +81,6 @@ # Default primary key field type DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + + +SESSION_EXPIRE_AT_BROWSER_CLOSE = True \ No newline at end of file diff --git a/main/urls.py b/main/urls.py index 7dc8666..8181a90 100644 --- a/main/urls.py +++ b/main/urls.py @@ -6,7 +6,8 @@ urlpatterns = [ path('admin/', admin.site.urls), - path('', LandingPageView.as_view(), name='landing-page') + path('', LandingPageView.as_view(), name='landing-page'), + path('core/', include('core.urls', namespace='core')), ] if settings.DEBUG: diff --git a/templates/landing_page.html b/templates/landing_page.html index 83160eb..58d3a31 100644 --- a/templates/landing_page.html +++ b/templates/landing_page.html @@ -1 +1,6 @@ -

Landing Page

\ No newline at end of file +

Landing Page

+{% if user.is_authenticated %} +

{{user.email}}

+{% else %} +

Not logged in

+{% endif %} \ No newline at end of file