Created
October 8, 2015 23:40
-
-
Save nicholasRutherford/6a3db68e51108f6d1a44 to your computer and use it in GitHub Desktop.
Example of Flask-breadcrumbs using MethodViews and blueprints
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template, Blueprint | |
from flask.ext import breadcrumbs | |
from flask.views import MethodView | |
app = Flask(__name__) | |
breadcrumbs.Breadcrumbs(app=app) | |
bp = Blueprint('bp', __name__,) | |
class LevelOneView(MethodView): | |
def get(self): | |
return render_template("template.html") | |
class LevelTwoView(MethodView): | |
def get(self): | |
return render_template("template.html") | |
# Define the view by calling the decorator on its own, | |
# followed by the view inside parenthesis | |
level_one_view = breadcrumbs.register_breadcrumb(bp, 'breadcrumbs.', 'Level One')(LevelOneView.as_view('first')) | |
bp.add_url_rule("/one", view_func=level_one_view) # Add the rule to the blueprint | |
level_two_view = breadcrumbs.register_breadcrumb(bp, 'breadcrumbs.two', 'Level Two')(LevelOneView.as_view('second')) | |
bp.add_url_rule("/two", view_func=level_two_view) | |
app.register_blueprint(bp) | |
if __name__ == '__main__': | |
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
</head> | |
<body> | |
<h1>Example</h1> | |
<div> | |
{%- for breadcrumb in breadcrumbs -%} | |
<a href="{{ breadcrumb.url }}">{{ breadcrumb.text }}</a> | |
{{ '/' if not loop.last }} | |
{%- endfor -%} | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment