Skip to content

Commit

Permalink
Implemented login page features along wit simple template
Browse files Browse the repository at this point in the history
  • Loading branch information
joisemp committed Apr 1, 2024
1 parent 46bd098 commit 7680e68
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 4 deletions.
12 changes: 12 additions & 0 deletions core/forms.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 6 additions & 0 deletions core/templates/core/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Login</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
8 changes: 8 additions & 0 deletions core/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path
from .import views

app_name = 'core'

urlpatterns = [
path('login/', views.LoginView.as_view(), name='login'),
]
19 changes: 17 additions & 2 deletions core/views.py
Original file line number Diff line number Diff line change
@@ -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'
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')
3 changes: 3 additions & 0 deletions main/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,6 @@
# Default primary key field type

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


SESSION_EXPIRE_AT_BROWSER_CLOSE = True
3 changes: 2 additions & 1 deletion main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 6 additions & 1 deletion templates/landing_page.html
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
<h1>Landing Page</h1>
<h1>Landing Page</h1>
{% if user.is_authenticated %}
<p>{{user.email}}</p>
{% else %}
<p>Not logged in</p>
{% endif %}

0 comments on commit 7680e68

Please sign in to comment.