Skip to content

Commit

Permalink
Login and Logout
Browse files Browse the repository at this point in the history
  • Loading branch information
norm committed Jul 27, 2021
1 parent 281484c commit 211ac80
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 12 deletions.
15 changes: 9 additions & 6 deletions hasworn/urls.py
Expand Up @@ -16,15 +16,18 @@
from django.contrib import admin
from django.urls import path

from hasworn.views import Home
from hasworn.views import Home, Login, Logout


urlpatterns = [
path('admin/', admin.site.urls),

# path(
# '',
# Home.as_view(),
# name='homepage'
# ),
path('login/', Login.as_view(), name = 'login'),
path('logout/', Logout.as_view(), name = 'logout'),

path(
'',
Home.as_view(),
name = 'homepage'
),
]
34 changes: 34 additions & 0 deletions hasworn/views.py
@@ -1,5 +1,39 @@
from django.contrib.auth import views, logout
from django.http import HttpResponseRedirect
from django.urls import path, reverse
from django.utils.decorators import method_decorator
from django.views.decorators.cache import never_cache
from django.views.generic import TemplateView


class Home(TemplateView):
template_name = 'home.html'


class Login(views.LoginView):
template_name='login.html'

def get_success_url(self):
return reverse('homepage')


class Logout(views.LogoutView):
template_name='logout.html'

def post(self, request, *args, **kwargs):
logout(request)
return HttpResponseRedirect(reverse('homepage'))

@method_decorator(never_cache)
def dispatch(self, request, *args, **kwargs):
# Standard View class dispatcher.
# (Don't logout on GET, only POST)
if request.method.lower() in self.http_method_names:
handler = getattr(
self,
request.method.lower(),
self.http_method_not_allowed
)
else:
handler = self.http_method_not_allowed
return handler(request, *args, **kwargs)
9 changes: 9 additions & 0 deletions templates/base_template.html
@@ -0,0 +1,9 @@
<html>
<head>

</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
37 changes: 31 additions & 6 deletions templates/home.html
@@ -1,6 +1,31 @@
<html>
<body>
<h1>hasworn.com temporary holding page</h1>
<p>Testing deploys</p>
</body>
</html>
{% extends "base_template.html" %}

{% block content %}
{% if user.is_authenticated %}
<form method='post' action='{% url "logout" %}'>
{% csrf_token %}
<input type='submit' value='Logout'>
</form>
{% else %}

<form method='post' action='{% url "login" %}'>
{% csrf_token %}
<label>
Username:
<input type='text' name='username' autofocus='' autocapitalize='none' autocomplete='username' maxlength='30' required='' id='id_username'>
</label>
<label>
Password:
<input type='password' name='password' autocomplete='current-password' required='' id='id_password'>
</label>
<input type='submit' value='login'>
</form>

{% comment %}
{# Assumes you setup the password_reset view in your URLconf #}
<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
{% endcomment %}

{% endif %}

{% endblock %}
40 changes: 40 additions & 0 deletions templates/login.html
@@ -0,0 +1,40 @@
{% extends "base_template.html" %}

{% block content %}

{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}

{% if next %}
{% if user.is_authenticated %}
<p>Your account doesn't have access to this page. To proceed,
please login with an account that has access.</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}

<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>

<input type="submit" value="login">
<input type="hidden" name="next" value="{{ next }}">
</form>

{% comment %}
{# Assumes you setup the password_reset view in your URLconf #}
<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
{% endcomment %}

{% endblock %}
12 changes: 12 additions & 0 deletions templates/logout.html
@@ -0,0 +1,12 @@
{% extends "base_template.html" %}

{% block content %}
{% if user.is_authenticated %}
<form method='post' action='{% url "logout" %}'>
{% csrf_token %}
<button type='submit'>Logout</button>
</form>
{% else %}
<p>Already logged out.</p>
{% endif %}
{% endblock %}

0 comments on commit 211ac80

Please sign in to comment.