Add basic login/logout pages and nav items

This commit is contained in:
Adam Goldsmith 2020-12-14 13:26:47 -05:00
parent f475e1bd0e
commit 3cd95443e6
5 changed files with 69 additions and 2 deletions

View File

@ -101,6 +101,8 @@ AUTH_PASSWORD_VALIDATORS = [
}, },
] ]
# Default URL to redirect to after authentication
LOGIN_REDIRECT_URL = '/'
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/ # https://docs.djangoproject.com/en/3.1/topics/i18n/

View File

@ -16,9 +16,18 @@ Including another URLconf
from django.contrib import admin from django.contrib import admin
from django.shortcuts import redirect from django.shortcuts import redirect
from django.urls import include, path from django.urls import include, path
from django.contrib.auth.views import LoginView, LogoutView
urlpatterns = [ urlpatterns = [
path('', lambda request: redirect('/tasks/')), path('', lambda request: redirect('/tasks/')),
path('tasks/', include('tasks.urls')), path('tasks/', include('tasks.urls')),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('auth/', include([
path('login/', LoginView.as_view(
template_name="auth/login.djhtml",
redirect_authenticated_user=True), name='login'),
path('logout/', LogoutView.as_view(template_name="auth/logout.djhtml"), name='logout'),
]))
] ]

View File

@ -0,0 +1,35 @@
{% extends "base.djhtml" %}
{% block title %} Login {% endblock %}
{% 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 class="d-flex flex-column align-items-center p-4" 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>
{% endblock %}

View File

@ -0,0 +1,10 @@
{% extends "base.djhtml" %}
{% block title %} Logout {% endblock %}
{% block content %}
<div class="d-flex flex-column align-items-center p-4">
<p>Logged out!</p>
<a href="{% url 'login'%}">Click here to login again.</a>
</div>
{% endblock %}

View File

@ -12,9 +12,20 @@
<body> <body>
<nav class="navbar navbar-expand-lg navbar-light bg-light"> <nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="/">RecMaint</a> <a class="navbar-brand" href="/">RecMaint</a>
<div class="collapse navbar-collapse">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#user-nav" aria-controls="user-nav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div id="user-nav" class="collapse navbar-collapse">
<div class="navbar-nav ml-auto"> <div class="navbar-nav ml-auto">
<a class="nav-item nav-link" href="/admin"> Admin </a> {% if user.is_staff %}
<a class="nav-item nav-link" href="/admin"> Admin </a>
{% endif %}
{% if user.is_authenticated %}
<a class="nav-item nav-link" href="/auth/logout"> Logout: {{ user }} </a>
{% else %}
<a class="nav-item nav-link" href="/auth/login"> Login </a>
{% endif %}
</div> </div>
</div> </div>
</nav> </nav>