所以你是在“路径/历史”面包屑之后,而不是“位置”面包屑来使用维基百科文章中的术语?
如果您想访问用户访问链接的历史记录,那么您将不得不将它们保存在会话中。我已经尝试过创建一个装饰器来做到这一点。
breadcrumb.py
:
import functools
import collections
import flask
BreadCrumb = collections.namedtuple('BreadCrumb', ['path', 'title'])
def breadcrumb(view_title):
def decorator(f):
@functools.wraps(f)
def decorated_function(*args, **kwargs):
# Put title into flask.g so views have access and
# don't need to repeat it
flask.g.title = view_title
# Also put previous breadcrumbs there, ready for view to use
session_crumbs = flask.session.setdefault('crumbs', [])
flask.g.breadcrumbs = []
for path, title in session_crumbs:
flask.g.breadcrumbs.append(BreadCrumb(path, title))
# Call the view
rv = f(*args, **kwargs)
# Now add the request path and title for that view
# to the list of crumbs we store in the session.
flask.session.modified = True
session_crumbs.append((flask.request.path, view_title))
# Only keep most recent crumbs (number should be configurable)
if len(session_crumbs) > 3:
session_crumbs.pop(0)
return rv
return decorated_function
return decorator
这是一个演示它的测试应用程序。请注意,我刚刚使用了 Flask 的内置客户端会话,您可能希望在生产中使用更安全的服务器端会话,例如Flask-KVsession。
#!/usr/bin/env python
import flask
from breadcrumb import breadcrumb
app = flask.Flask(__name__)
@app.route('/')
@breadcrumb('The index page')
def index():
return flask.render_template('page.html')
@app.route('/a')
@breadcrumb('Aardvark')
def pagea():
return flask.render_template('page.html')
@app.route('/b')
@breadcrumb('Banana')
def pageb():
return flask.render_template('page.html')
@app.route('/c')
@breadcrumb('Chimp')
def pagec():
return flask.render_template('page.html')
@app.route('/d')
@breadcrumb('Donkey')
def paged():
return flask.render_template('page.html')
if __name__ == '__main__':
app.secret_key = '83cf5ca3-b1ee-41bb-b7a8-7a56c906b05f'
app.debug = True
app.run()
这是以下内容templates/page.html
:
<!DOCTYPE html>
<html>
<head><title>{{ g.title }}</title></head>
<body>
<h1>{{ g.title }}</h1>
<p>Breadcrumbs:
{% for crumb in g.breadcrumbs %}
<a href="{{ crumb.path }}">{{ crumb.title }}</a>
{% if not loop.last %}»{% endif %}
{% endfor %}
</p>
<p>What next?</p>
<ul>
<li><a href="/a">Aardvark</a>?</li>
<li><a href="/b">Banana</a>?</li>
<li><a href="/c">Chimp</a>?</li>
<li><a href="/d">Donkey</a>?</li>
</ul>
</body>
</html>