Herramientas de usuario

Herramientas del sitio


wiki2:python:flask

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anterior Revisión previa
Próxima revisión
Revisión previa
wiki2:python:flask [2015/08/09 14:40]
alfred [Pluggable views]
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 156: Línea 165:
 </​code>​ </​code>​
 So you can implement a REST API easily: So you can implement a REST API easily:
-URL | Method ​+URL Method ​^
 | /users/ | GET |  | /users/ | GET | 
 | /users/ | POST | | /users/ | POST |
Línea 206: Línea 215:
 </​code>​ </​code>​
 ==== Blueprints ==== ==== Blueprints ====
 +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>
 +from flask import Blueprint, render_template
 +
 +profile = Blueprint('​profile',​ __name__)
 +
 +@profile.route('/<​user_url_slug>'​)
 +def timeline(user_url_slug):​
 +    # Do some stuff
 +    return render_template('​profile/​timeline.html'​)
 +
 +@profile.route('/<​user_url_slug>/​photos'​)
 +def photos(user_url_slug):​
 +    # Do some stuff
 +    return render_template('​profile/​photos.html'​)
 +
 +@profile.route('/<​user_url_slug>/​about'​)
 +def about(user_url_slug):​
 +    # Do some stuff
 +    return render_template('​profile/​about.html'​)
 +</​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 218: 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>​
wiki2/python/flask.1439131220.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)