Enable breadcrumb support, use it on the login view

This commit is contained in:
Danielle McLean 2017-10-25 00:25:41 +11:00
parent c210650ca7
commit 84a34c89e7
Signed by: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
7 changed files with 57 additions and 3 deletions

View file

@ -1,5 +1,8 @@
from django.shortcuts import get_object_or_404, render
from users.models import User
from lemoncurry import breadcrumbs
breadcrumbs.add('home:index', 'home')
def index(request):

View file

@ -1,4 +1,7 @@
from django.contrib.auth import views as auth_views
from lemoncurry import breadcrumbs
breadcrumbs.add(route='lemonauth:login', label='login', parent='home:index')
login = auth_views.LoginView.as_view(
template_name='lemonauth/login.html',

15
lemoncurry/breadcrumbs.py Normal file
View file

@ -0,0 +1,15 @@
breadcrumbs = {}
def add(route, label, parent=None):
breadcrumbs[route] = {'label': label, 'route': route, 'parent': parent}
def find(route):
crumbs = []
while route:
crumb = breadcrumbs[route]
crumbs.append(crumb)
route = crumb['parent']
crumbs.reverse()
return crumbs

View file

@ -20,9 +20,16 @@ body
background-color $base00
color $base07
> header
background-color $base01
#navbar
justify-content space-between
> .navbar
background-color $base01
#navbar
justify-content space-between
> .breadcrumbs
background-color $base02
> .breadcrumb
background-color transparent
border-radius 0
> main
margin 2rem

View file

@ -29,6 +29,9 @@
{% nav_right request %}
</div>
</nav>
{% if request.resolver_match.view_name %}
{% nav_crumbs request.resolver_match.view_name %}
{% endif %}
</header>
<main>

View file

@ -0,0 +1,14 @@
{% if crumbs %}
<nav class="breadcrumbs" aria-label="breadcrumb" role="navigation">
<ol class="breadcrumb">
{% for crumb in crumbs %}
<li class="breadcrumb-item">
<a href="{% url crumb.route %}">{{ crumb.label }}</a>
</li>
{% endfor %}
<li class="breadcrumb-item active" aria-current="page">
{{ current.label }}
</li>
</ol>
</nav>
{% endif %}

View file

@ -7,6 +7,8 @@ from django import template
from django.conf import settings
from django.urls import reverse
from .. import breadcrumbs
register = template.Library()
cache = SimpleNamespace(package_json=None)
@ -50,3 +52,10 @@ def nav_right(request):
MenuItem(label='log in', icon='fa fa-sign-in', url='lemonauth:login'),
)
return {'items': items}
@register.inclusion_tag('lemoncurry/tags/breadcrumbs.html')
def nav_crumbs(route):
crumbs = breadcrumbs.find(route)
current = crumbs.pop()
return {'crumbs': crumbs, 'current': current}