所以你是在「路徑/歷史」麪包屑,而不是「位置」麪包屑使用術語從wikipedia article?
如果你想訪問用戶的訪問過的鏈接歷史,那麼你將不得不將它們保存在一個會話中。我已經創建了一個裝飾器來做到這一點。
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>
來源
2012-11-30 00:07:07
Day
這是真棒,但如果你正在嘗試做的「位置」基於食物殘渣呢?你能否爲此添加一個答案?我認爲這會容易得多。 – chrickso
@chrisckso:我認爲基於位置的麪包屑是[bread.py](http://russell.ballestrini.net/a-homegrown-python-bread-crumb-module/)(如[問題]中提到的) (http://stackoverflow.com/q/13444666/445073))呢?你嘗試過嗎? – Day
是的,我看着它,但我不是它的粉絲,只是拉下網址,因爲我想定製的話是什麼。我正在尋找最簡單的方法來做一個像@ breadtrail(['Crumb A',url_for('...)],['Crumb B',url_for('...)])的裝飾器,並且總是通過breadtrail var將這些數據填充到jinja,而無需爲每條路線輸入breadtrail = breadtrail。 – chrickso