Getting flask (1) python

Reference: https://blog.csdn.net/freeking101/article/details/100174215

 

 

File Upload

Flask deal with file uploads is simple. Just make sure you do not forget to set the enctype = "multipart / form-data" attribute in an HTML form, or your browser will not send the file.

Uploaded files are stored in a temporary memory location or file system. You can access them through the files requested attributes of the object. Uploaded files will be stored in each of the dictionary. It behaves almost as a standard Python file object, but it also has a  save ()  method, which allows you to save files to the server's file system. Here is an example of using it to save the file:

from flask import request
 
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['the_file']
        f.save('/var/www/uploads/uploaded_file.txt')

If you want to know what the file on the client before uploading the file name, you can access the  filename  property. But remember, never trust the value that can be forged. If the file name you want to store the file provided by the client on the server, then please pass it to Werkzeug provided  secure_filename ()  function:

from flask import request
from werkzeug import secure_filename
 
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['the_file']
        f.save('/var/www/uploads/' + secure_filename(f.filename))

Some better examples, see  upload files  mode

 

Redirection and error

 

You can use the  redirect ()  function to redirect the user to other places. Abandoning the request and return an error code, using  abort ()  function. Here is an example of how they are used:

from flask import abort, redirect, url_for
 
@app.route('/')
def index():
    return redirect(url_for('login'))
 
@app.route('/login')
def login():
    abort(401)
    this_is_never_executed()

 

This is an example of a fairly meaningless because the user will be redirected to a page from the home page can not be accessed (401 implies a ban on access), but it shows how redirection works.

By default, the error code will display a black and white error page. If you want to customize error pages, you can use  errorhandler ()  decorator:

 

from flask import render_template
 
@app.errorhandler(404)
def page_not_found(error):
    return render_template('page_not_found.html'), 404

Note  render_template ()  404 after the call. This tells Flask, page error code is 404, that is not found. The default is 200, that is, everything is normal.

 

 

 

 

Template Rendering

Generate HTML in Python is very boring, and quite tedious because you have to manually do HTML escaping to ensure the safety of the application. To this end, Flask equipped with  Jinja2  template engine.

You can use  render_template ()  method to render a template. Everything you need to do is name the template and you want to pass as a parameter keyword variables templates. Here's a simple example to show how to render the template:

from flask import render_template
 
@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
    return render_template('hello.html', name=name)

Flask will look for templates in the templates folder. So, if your application is a module, this folder should be the same level with the module; if it is a package, then this folder as a package subdirectory:

 

 

 

 

About templates, you can play all instances Jinja2 template. For more information see  Jinja2 template document  .

Here is an example template:

<!doctype html>
<title>Hello from Flask</title>
{% if name %}
  <h1>Hello {{ name }}!</h1>
{% else %}
  <h1>Hello World!</h1>
{% endif %}

 

In the template, you can also access the  Request  ,  the session  and  G  [1]  objects, and  get_flashed_messages ()  function.

Template inheritance with them quite easily make a template. To understand the working mechanism of inheritance, skip to  template inheritance  document mode. At a minimum, the template can inherit specific elements (such as headers, footers, and navigation bars) may appear on all pages.

Automatic escaping is enabled by default, so if the name contains HTML, it will be automatically escaped. If you can trust a variable, and you know it is secure (for example, a module to convert Wiki markup to HTML), you can use the Markup class or | safe filter template to mark it as safe. In Jinja 2 documentation, you will see more examples.

 

Guess you like

Origin www.cnblogs.com/lshan/p/11649564.html