New files
templates/registration/login.html
{% extends "app/base.html" %}
{% block content %}
<h1>Log in</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Log in</button>
</form>
{% endblock content %}
Modified files
djangocourse/settings.py
---
+++
@@ -119,6 +119,9 @@
]
+LOGIN_REDIRECT_URL = "home"
+LOGOUT_REDIRECT_URL = "login"
+
# Internationalization
# https://docs.djangoproject.com/en/5.1/topics/i18n/
djangocourse/urls.py
---
+++
@@ -20,5 +20,6 @@
urlpatterns = [
path("admin/", admin.site.urls),
path("articles/", include("app.urls")),
+ path("accounts/", include("django.contrib.auth.urls")),
path("__debug__/", include("debug_toolbar.urls"))
]
templates/app/base.html
---
+++
@@ -6,7 +6,21 @@
<title>Djangocourse</title>
</head>
<body>
- <nav>Common navigation bar for all templates</nav>
+ <nav>
+ <ul>
+ <li><a href="{% url 'home' %}">Articles</a></li>
+ <li>
+ {% if user.is_authenticated %}
+ <form method="POST" action="{% url 'logout' %}">
+ {% csrf_token %}
+ <button type="submit">Log out</button>
+ </form>
+ {% else %}
+ <a href="{% url 'login' %}">Log in</a>
+ {% endif %}
+ </li>
+ </ul>
+ </nav>
{% block content %}
{% endblock content %}