flask前后端不分离传服务器任意目录到前端显示
网址:https://blog.csdn.net/dejing6575/article/details/101474297
static_folder表示静态文件所在路径,默认为root_dir下的static文件夹
static_url_path的行为比较复杂
如果static_folder未被指定(也就是默认值static),那么static_url_path取为static
如果static_folder被指定了,那么static_url_path等于static_folder的最后一级文件夹名称。
手动指定static_url_path时,如果static_url_path不为空串,url的路径必须以/开头,如/static。
手动指定static_url_path时,如果static_url_path为空串,url路径不必以/开头,否则相当于static_url_path=None的情况,也就是使用static_folder的目录名字。
static_path即将废弃,推荐使用static_path_url
代码:
from flask import Flask, render_template, request, redirect
import os
father_path = "D:\img"
app = Flask(name,
static_folder=father_path
)
folder_name = father_path
def picture_path(path, all_files=[]):
files = os.listdir(path)
for file in files:
if not os.path.isdir(os.path.join(path, file)):
all_files.append(os.path.join(path, file))
else:
picture_path(os.path.join(path, file), all_files)
return all_files
@app.route('/writer/img
def writer_img(path):
print(path)
return render_template("display_img.html")
@app.route("/display/img/
def display_img(msg):
date_folder = os.path.join(folder_name, msg)
all_file_list = picture_path(date_folder)
img_name_png_list = []
for file_name in all_file_list:
if file_name.endswith(".png"):
img_name_png_list.append(file_name)
display_name_list = []
for img_name in img_name_png_list:
display_name_list.append(img_name.split("😊[1])
return render_template("display_img.html",img_png_list=display_name_list)
@app.route('/', methods=["GET"])
def index():
folder_name_list = os.listdir(folder_name)
return render_template('index.html', folder_list=folder_name_list)
if name == 'main':
app.run()

浙公网安备 33010602011771号