From 2db9b400dc1dbd5d0e1f3fa829d3d9029540b051 Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Fri, 4 Mar 2022 13:46:41 -0500 Subject: [PATCH] Nicer display of currently logged-in user --- src/core/templates/core/base.html | 4 ++++ src/user_profile/urls.py | 9 ++++++++- src/user_profile/views.py | 11 ++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/core/templates/core/base.html b/src/core/templates/core/base.html index b1e0ece..f304cfd 100644 --- a/src/core/templates/core/base.html +++ b/src/core/templates/core/base.html @@ -34,6 +34,10 @@ {% else %} Training {% endif %} + + {% if request.user.is_authenticated %} + Logged in as {{ request.user.username }}. + {% endif %} {% block content %}{% endblock %} diff --git a/src/user_profile/urls.py b/src/user_profile/urls.py index bc2c6d8..d1dad9b 100644 --- a/src/user_profile/urls.py +++ b/src/user_profile/urls.py @@ -2,10 +2,17 @@ from django.urls import path from . import views +app_name = 'user' + urlpatterns = ( + path( + '', + views.user_profile_detail, + name='self-profile-detail', + ), path( '', views.user_profile_detail, - name='user-profile-detail', + name='profile-detail', ), ) diff --git a/src/user_profile/views.py b/src/user_profile/views.py index 41498b6..39557a9 100644 --- a/src/user_profile/views.py +++ b/src/user_profile/views.py @@ -1,4 +1,5 @@ from django.contrib.auth.models import User +from django.http import Http404 from django.shortcuts import get_object_or_404 from django.views.generic.detail import DetailView @@ -6,6 +7,14 @@ class UserProfileDetailView(DetailView): model = User def get_object(self): - return get_object_or_404(User, username=self.kwargs['username']) + username = self.kwargs.get('username') + + if username: + return get_object_or_404(User, username=username) + + if self.request.user.is_authenticated: + return self.request.user + + raise Http404() user_profile_detail = UserProfileDetailView.as_view() -- 2.20.1