Django authentication and social-sign on with django-allauth

How to add log in and log out pages with Django

Want more?

This lesson for enrolled students only. Join the course to unlock it!

You can see the code changes implemented in this lecture below.

If you have purchased the course in a different platform, you still have access to the code changes per lecture here on Teclado. The lecture video and lecture notes remain locked.
Join course for $15

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 %}