From 7153911d62cdfd592fe1a43689ae041b7c59c865 Mon Sep 17 00:00:00 2001 From: Petri Riihikallio Date: Tue, 10 Aug 2021 15:22:32 +0300 Subject: [PATCH] Extend logout drop down for customizations --- docs/topics/browsable-api.md | 37 ++++++++++++++++++- rest_framework/templatetags/rest_framework.py | 29 +++++++++------ 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/docs/topics/browsable-api.md b/docs/topics/browsable-api.md index ed70c4901..9b541287c 100644 --- a/docs/topics/browsable-api.md +++ b/docs/topics/browsable-api.md @@ -88,7 +88,9 @@ All of the standard [Bootstrap components][bcomponents] are available. The browsable API makes use of the Bootstrap tooltips component. Any element with the `js-tooltip` class and a `title` attribute has that title content will display a tooltip on hover events. -### Login Template +### Customizing Authentication + +#### Login Template To add branding and customize the look-and-feel of the login template, create a template called `login.html` and add it to your project, eg: `templates/rest_framework/login.html`. The template should extend from `rest_framework/login_base.html`. @@ -102,6 +104,39 @@ You can add your site name or branding by including the branding block: You can also customize the style by adding the `bootstrap_theme` or `style` block similar to `api.html`. +#### Login/Logout Button + +The Login/Logout functionality comes from userlinks block from 'base.html' and 'admin.html' templates. If you want to remove it then you can just override the block in both templates: + + {% extends "rest_framework/base.html" %} + + {% block userlinks %} + {% endblock %} + +If you want to extend the logout drop down menu then add the following to both project templates: + + {% extends "rest_framework/base.html" %} + {% load rest_framework %} + + {% block userlinks %} + {% if user.is_authenticated %} + + {% else %} +
  • {% optional_login request %}
  • + {% endif %} + {% endblock %} + ### Advanced Customization #### Context diff --git a/rest_framework/templatetags/rest_framework.py b/rest_framework/templatetags/rest_framework.py index db0e9c95c..34423c801 100644 --- a/rest_framework/templatetags/rest_framework.py +++ b/rest_framework/templatetags/rest_framework.py @@ -120,27 +120,32 @@ def optional_docs_login(request): @register.simple_tag -def optional_logout(request, user): +def optional_logout(request, user=""): """ Include a logout snippet if REST framework's logout view is in the URLconf. """ try: logout_url = reverse('rest_framework:logout') except NoReverseMatch: - snippet = format_html('', user=escape(user)) + snippet = format_html('', user=escape(user) or 'logged in') return mark_safe(snippet) - snippet = """""" - snippet = format_html(snippet, user=escape(user), href=logout_url, next=escape(request.path)) + if user: + snippet = """""" +# snippet = format_html(snippet, user=escape(user), href=logout_url, next=escape(request.path)) + else: + snippet = "Log out" +# snippet = format_html(snippet, href=logout_url, next=escape(request.path)) + snippet = format_html(snippet, user=escape(user), href=logout_url, next=escape(request.path)) return mark_safe(snippet)