Flask points of knowledge

Flash achieves a flash:

1. In the mvc design pattern, in C, that is, in the routing configuration function of the app, we
1

@app.route("/flash/")
def flashtest():
    flash('有问题')
    return render_template('flashtest.html')
  1. In flashtest.html, we get all flashes through get_flashed_messages(), and you can also tag flash in the get_flashed_messages() function
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% for msg in get_flashed_messages()  %}
    <h1>{
   
   { msg }}</h1>
{% endfor %}

</body>
</html>

3 Do not forget to add an app.secret_key

app.secret_key = '521'  # 自己指定

app.secret_key=os.urandom(16)  # 随机生成

Two upload files

In the view control function, we obtain the passed file stream through request.files['the_file'], which is almost equivalent to python opening a local file stream, and then save it to a place f.save('uploads/uploaded_file. txt'), of course, this method does not actively create files

@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['the_file']
       # f1 = request.args.get('the_file')
       # print(type(f1))
       # print(f)
        f.save('uploads/uploaded_file.txt')
        return '上传成功'
    else:
        return render_template('uploadfile.html')

Introduction to three mvc design patterns

The design pattern is the template of the most effective solution for the predecessors to face specific scenarios

MVC solves the problem of data and display.
M is the model, that is, the encapsulated data
v is how the view is displayed, the view
c is the controller control, and the middleman between m and v

Almost all applications have to deal with data, so the mvc design pattern is also one of the most widely used design patterns

Four ways to keep the environment of different developers consistent

pip freeze> requirement.txt Import the local developer environment into the requirement.txt file

Then others get this file and place it in the corresponding directory,
pip install -r requirement.txt installs the library package in the file

Five forms

Acquisition of native forms, data acquisition and parameter verification of flask-encapsulated forms

Six Blueprints

Seven shows the picture in the returned webpage

In fact, this problem can be simplified to display the picture when accessing the address of a picture.

@app.route('/<img>', methods=('GET', 'POST'))
def mainpage(img):
    img_path = './images/'+img  
    print(img_path)
    with open(img_path, 'rb') as f:
        image = f.read()
    resp = Response(image, mimetype="image/jpeg")
    return resp

Requirement: When accessing imgs/route, display the content of all pictures in this folder, click on the corresponding picture, and then the corresponding picture
How to recognize the picture and text, if I change the suffix of the text to jpg?

Guess you like

Origin blog.csdn.net/xiaoxiaodechongzi/article/details/102714617