Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
wiki2:python:flask [2015/08/09 15:11] alfred [Blueprints] |
wiki2:python:flask [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 1: | Línea 1: | ||
| ====== Flask ====== | ====== Flask ====== | ||
| + | ===== Extensions ===== | ||
| + | |||
| + | * [[wiki2:python:flask:sqlalchemy|Flask SQLAlchemy]] | ||
| + | * [[wiki2:python:flask:restful|Flask RESTful]] | ||
| + | * [[wiki2:python:flask:mongoalchemy|Flask MongoAlchemy]] | ||
| + | * [[wiki2:python:flask:flaskmongoengine|Flask Mongo Engine]] | ||
| + | * [[wiki2:python:flask:flasklogin|Flask Login]] | ||
| + | * [[wiki2:python:flask:testing|Flask Testing]] | ||
| + | * [[wiki2:python:flask:used_libraries|Flask used libraries]] | ||
| ===== Some elements ===== | ===== Some elements ===== | ||
| Línea 208: | Línea 217: | ||
| A blueprint defines a collection of views, templates, static files and other elements that can be applied to an application. For example, let’s imagine that we have a blueprint for an admin panel. This blueprint would define the views for routes like /admin/login and /admin/dashboard. This lets us structure our app as several smaller “apps” that each do one thing. | A blueprint defines a collection of views, templates, static files and other elements that can be applied to an application. For example, let’s imagine that we have a blueprint for an admin panel. This blueprint would define the views for routes like /admin/login and /admin/dashboard. This lets us structure our app as several smaller “apps” that each do one thing. | ||
| <code python> | <code python> | ||
| - | # facebook/views/profile.py | ||
| - | |||
| from flask import Blueprint, render_template | from flask import Blueprint, render_template | ||
| Línea 229: | Línea 236: | ||
| return render_template('profile/about.html') | return render_template('profile/about.html') | ||
| </code> | </code> | ||
| + | If we were using a divisional structure, we’d want to tell Flask that the blueprint has its own template and static directories. | ||
| + | <code python> | ||
| + | profile = Blueprint('profile', __name__, | ||
| + | template_folder='templates', | ||
| + | static_folder='static') | ||
| + | </code> | ||
| + | Now we only need to add it to our app: | ||
| + | <code python> | ||
| + | # facebook/__init__.py | ||
| + | |||
| + | from flask import Flask | ||
| + | from .views.profile import profile | ||
| + | |||
| + | app = Flask(__name__) | ||
| + | app.register_blueprint(profile) | ||
| + | </code> | ||
| + | |||
| + | We can define the prefix in one of two places: \\ | ||
| + | (1) when we instantiate the Blueprint() | ||
| + | <code python> | ||
| + | from flask import Blueprint, render_template | ||
| + | |||
| + | profile = Blueprint('profile', __name__, url_prefix='/<user_url_slug>') | ||
| + | |||
| + | # [...] | ||
| + | </code> | ||
| + | (2) when we register it with app.register_blueprint() | ||
| + | <code python> | ||
| + | from flask import Flask | ||
| + | from .views.profile import profile | ||
| + | |||
| + | app = Flask(__name__) | ||
| + | app.register_blueprint(profile, url_prefix='/<user_url_slug>') | ||
| + | </code> | ||
| + | |||
| + | === Build blueprints URLs === | ||
| + | If you want to link from one page to another you can use the url_for() function just like you normally would do just that you prefix the URL endpoint with the name of the blueprint and a dot (.): | ||
| + | <code python> | ||
| + | url_for('admin.index') | ||
| + | </code> | ||
| + | Additionally if you are in a view function of a blueprint or a rendered template and you want to link to another endpoint of the same blueprint, you can use relative redirects by prefixing the endpoint with a dot only: | ||
| + | <code python> | ||
| + | url_for('.index') | ||
| + | </code> | ||
| + | |||
| + | === Notes === | ||
| + | To access the flask app from a blueprint: | ||
| + | <code python> | ||
| + | from flask import current_app | ||
| + | ... | ||
| + | if current_app.debug: | ||
| + | ... | ||
| + | </code> | ||
| + | |||
| + | * The url's will end with ''/''. | ||
| ===== Notes ===== | ===== Notes ===== | ||
| Línea 240: | Línea 302: | ||
| return redirect(url_for('static', filename='resume.html')) | return redirect(url_for('static', filename='resume.html')) | ||
| </code> | </code> | ||
| - | ''url_for(folder, file)'' will return the url for that file in that folder. | + | |
| + | ==== url_for ==== | ||
| + | |||
| + | ''url_for(folder, file)'' will return the url for that file in that folder. \\ | ||
| + | |||
| + | For static files: | ||
| + | <code python> | ||
| + | url_for('static', filename='path/to/file') | ||
| + | # In blueprints | ||
| + | url_for('admin.static', filename='style.css') | ||
| + | </code> | ||
| + | You also can use it in templates. | ||
| + | <code> | ||
| + | <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='bootstrap/bootstrap.min.css') }}"> | ||
| + | </code> | ||
| + | In blueprints: | ||
| + | <code python> | ||
| + | # this blueprint -> printable function | ||
| + | url_for('.printable') | ||
| + | # blueprint: resume, resource FUNCTION: printable_resume | ||
| + | url_for('resume.printable_resume') | ||
| + | </code> | ||
| + | You can get the absolute url: | ||
| + | <code python> | ||
| + | url_for('settings', _external=True) | ||
| + | </code> | ||