做打牌的网站怎么办,织梦模板网站好吗,wordpress单页面代码,外贸人才网论坛上一章的版本https://blog.csdn.net/weixin_44517278/article/details/135275066#xff0c;在Windows下debug完成无异常后#xff0c;上传到我的树莓下开始正式服役 由于开发环境是Windows#xff0c;使用环境是Linux#xff0c;导致最后没能成功运行起来 这个版本是今天去…上一章的版本https://blog.csdn.net/weixin_44517278/article/details/135275066在Windows下debug完成无异常后上传到我的树莓下开始正式服役 由于开发环境是Windows使用环境是Linux导致最后没能成功运行起来 这个版本是今天去debug完成了目前是Windows/Linux都能运行 问题点一
# 运行出现这个报错
# ERROR in app: Exception on /favicon.ico [GET]# 解法
# 添加 favicon.ico 请求的处理
self.app.route(/favicon.ico, methods[GET])(self.ignore_favicon)def ignore_favicon(self):# 忽略 /favicon.ico 请求返回 404return abort(404)问题点二
# 路径问题
# 发现返回的文件夹路径没有 根目录 ‘/’# 解法
# 添加 path: 让返回是路径self.app.route(/download/path:file_name)(self.download_file)self.app.route(/show_folder/path:folder_name)(self.show_folder)self.app.route(/return_folder/path:folder_name)(self.return_folder)# 判断假设没有根目录且不是Windows的文件路径结构就添加一个 / 在左边if not file_name.startswith(/):if not re.match(.:, file_name):file_name / file_name
问题点三
# 额外发现原本埋的Bug,在返回超两层目录时路径有问题修改完以后成下面这样if folder_name .:folder_name else:# 点击返回两次会报错发现这里有问题加上下面这句不全folder_name路径# 不能直接放在判断句外面否则进到初始目录还会显示返回链接点击会报错folder_name os.path.join(FileManagementApp.gDataPath, folder_name)
额外增加了上传路径可以自己选择的功能
以下是python源码和HTML模版 index.html
from flask import Flask, render_template, send_file, request, abort
import os, re# 定义类
class FileManagementApp:# 定义类变量这里是放数据库根目录我这里就是我树莓派系统上的存储盘挂载位置gDataPath os.path.normpath(/data/HOME_NAS/mydata)def __init__(self):self.app Flask(__name__)self.app.config[UPLOAD_FOLDER] # 添加basename方法让HTML中使用 路径|basename 输出结果是路径文件夹名或文件名而不是完整路径self.app.add_template_filter(self.basename)# 把所有下面函数定义的路由集中到这里清晰明了self.app.route(/)(self.mainweb)self.app.route(/show_item)(self.index)self.app.route(/download/path:file_name)(self.download_file)self.app.route(/show_folder/path:folder_name)(self.show_folder)self.app.route(/return_folder/path:folder_name)(self.return_folder)self.app.route(/upload, methods[POST])(self.upload_file)self.app.route(/search, methods[POST])(self.search_file)# linux BUG-1 : ERROR in app: Exception on /favicon.ico [GET]# 添加 favicon.ico 请求的处理self.app.route(/favicon.ico, methods[GET])(self.ignore_favicon)def basename(self, value):return os.path.basename(value)# 主页面show_mainTrue控制让它显示在html中避免HTML中所有模块都显示在网页中默认是关闭的参考HTML模版def mainweb(self):return render_template(index.html,show_mainTrue,show_uploadFalse)# 根据mainweb页面用户点击后的返回跳转到不同的页面show_xxxTrue 则是打开显示不同的模块其余不显示def index(self, show_item):if show_item show_list:files, folder_names, folder_name self.getfile()return render_template(index.html,filesfiles,folder_namesfolder_names,folder_namefolder_name,show_listTrue)elif show_item show_search:# files, folder_names, folder_name self.getfile()files, folder_names, folder_name [], [], return render_template(index.html,filesfiles,folder_namesfolder_names,folder_namefolder_name,show_searchTrue)elif show_item show_upload:files, folder_names, folder_name self.getfile()return render_template(index.html,filesfiles,folder_namesfolder_names,folder_namefolder_name,show_uploadTrue,show_listTrue)# 下载文件def download_file(self, file_name):# 替换windows系统路径 \\为 /即兼容不同系统的路径file_name os.path.normpath(file_name)# Linux Bug-1 add /if not file_name.startswith(/):if not re.match(.:, file_name):file_name / file_name# 将选择的文件下载下来return send_file(file_name, as_attachmentTrue)# 显示当前路径下所有的文件夹和文件不包含子目录下的def show_folder(self, folder_name):# Linux Bug-1 add /# Linux Bug-1 add /if not folder_name.startswith(/):if not re.match(.:, folder_name):folder_name / folder_namefiles, folder_names, folder_name self.getfile(folder_name)return render_template(index.html,filesfiles,folder_namesfolder_names,folder_namefolder_name,show_listTrue)# 返回上级目录def return_folder(self, folder_name):# Linux Bug-1 add /if not folder_name.startswith(/):if not re.match(.:, folder_name):folder_name / folder_namerefolder folder_namefull_path os.path.join(FileManagementApp.gDataPath, refolder)for root, dirs, files in os.walk(FileManagementApp.gDataPath, topdownTrue):for dir in dirs:if os.path.join(root, dir) full_path:folder_name os.path.relpath(root, startFileManagementApp.gDataPath)print(folder_name)if folder_name .:folder_name else:# 点击返回两次会报错发现这里有问题加上下面这句不全folder_name路径# 不能直接放在判断句外面否则进到初始目录还会显示返回链接点击会报错folder_name os.path.join(FileManagementApp.gDataPath, folder_name)files, folder_names, folder_name self.getfile(folder_name)return render_template(index.html,filesfiles,folder_namesfolder_names,folder_namefolder_name,show_listTrue)# 抓取指定路径下所有的文件文件夹不包含子文件夹下的内容def getfile(self, folder_name):files []folder_names []full_path os.path.join(FileManagementApp.gDataPath, folder_name)fileList os.listdir(full_path)for file in fileList:file os.path.join(full_path, file)file os.path.normpath(file)if os.path.isfile(file):files.append(file)else:folder_names.append(file)return files, folder_names, folder_name# 上传文件上传的路径就是现在进到的路径不允许在网页创建新的目录文件夹def upload_file(self):# 读取网页返回的值file request.files[file]folder_name request.form[folder_name]UPLOAD_FOLDER os.path.join(FileManagementApp.gDataPath, folder_name)self.app.config[UPLOAD_FOLDER] UPLOAD_FOLDERif file not in request.files:return No file partfile request.files[file]if file.filename :return No selected file# 将文件保存到指定路径下file.save(os.path.join(self.app.config[UPLOAD_FOLDER], file.filename))files, folder_names, folder_name self.getfile(folder_name)# 维持在这个上传文件的路径return render_template(index.html,filesfiles,folder_namesfolder_names,folder_namefolder_name,show_listTrue)# 查找文件def search_file(self):sfile_result []sfolder_result []# keyword request.text[keyword] ## errorkeyword request.form.get(keyword, ) # 获取名为 keyword 的表单字段的值if keyword :passelse:files, folder_names self.perform_search_file()for file in files:if keyword in file:sfile_result.append(file)for sfolder in folder_names:if keyword in sfolder:sfolder_result.append(sfolder)return render_template(index.html,filessfile_result,folder_namessfolder_result,folder_name,show_searchTrue,show_uploadFalse)# 执行搜索的功能遍历存储路径下所有的文件看是否有包含关键字的文件并返回def perform_search_file(self):file_result []folder_result []for root, dirs, files in os.walk(FileManagementApp.gDataPath, topdownTrue):for file in files:full_path os.path.join(root, file)full_path os.path.normpath(full_path)file_result.append(full_path)for dir in dirs:full_path os.path.join(root, dir)full_path os.path.normpath(full_path)folder_result.append(full_path)return file_result, folder_result# soltion BUG-1:def ignore_favicon(self):# 忽略 /favicon.ico 请求返回 404return abort(404)# 运行服务def run(self):self.app.run(host0.0.0.0, port5000)if __name__ __main__:# 实例化并开始执行file_app FileManagementApp()file_app.run()!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleFolder Viewer/titlestylebody {font-family: Arial, sans-serif;margin: 20px;}h1 {color: #333;}p {margin-bottom: 10px;}form {margin-bottom: 20px;}ul {list-style: none;padding: 0;}li {margin-bottom: 5px;}a {text-decoration: none;color: #007BFF;}a:link {color:#110101;} /* 未访问链接*/a:visited {color:#00FF00;} /* 已访问链接 */a:hover {color:#FF00FF;} /* 鼠标移动到链接上 */a:active {color:#0000FF;} /* 鼠标点击时 */h2 {margin-top: 20px;color: #333;}/style
/head
body!--show_main|default(false) 设定这个show_main的默认值是false,就是假设没传参进来就是false的--
{% if show_main|default(false) %}h1欢迎进入JRP系统主页/h1
h1当前版本 1.1.2023.12.29/h1
h1作者零时搞学习/h1
h1/h1lia href{{ url_for(index, show_itemshow_upload) }}上传/a/lilia href{{ url_for(index, show_itemshow_list) }}浏览/a/lilia href{{ url_for(index, show_itemshow_search) }}搜索/a/li
{% endif %}{% if show_upload|default(true) %}form methodpost enctypemultipart/form-data action/uploadinput typefile namefile!--隐藏项不会显示但是可以返回folder_name值给脚本--input typehidden namefolder_name value{{ folder_name }}input typesubmit valueUpload/form
{% endif %}{% if show_search|default(false) %}lia href{{ url_for(mainweb) }}首页/a/lih1文件搜索/h1form action/search methodpostinput typetext namekeyword placeholder输入关键字button typesubmit搜索/button/formulh2搜索结果/h2h2文件夹:/h2{% for foldername in folder_names %}lia href{{ url_for(search_file, folder_namefoldername) }}{{ foldername|basename }}/a/li{% endfor %}h2文件:/h2{% for filename in files %}lia href{{ url_for(download_file, file_namefilename) }} download{{ filename|basename }}/a/li{% endfor %}/ul
{% endif %}{% if show_list|default(false) %}lia href{{ url_for(mainweb) }}首页/a/lih1文件下载列表/h1{% if folder_name %}p当前路径/p{% else %}p当前路径:/plia href{{ url_for(return_folder, folder_namefolder_name) }}返回{{ folder_name|basename }}/a/li{% endif %}ulh2文件夹:/h2{% for foldername in folder_names %}lia href{{ url_for(show_folder, folder_namefoldername) }}{{ foldername|basename }}/a/li{% endfor %}h2文件:/h2{% for filename in files %}lia href{{ url_for(download_file, file_namefilename) }} download{{ filename|basename }}/a/li{% endfor %}/ul
{% endif %}
/body
/html
承接上文在这个页面 点击文件夹进入子文件夹下并显示这个文件夹下的资料点击文件可以直接下载 进入新路径结果如下点击返回可以返回上级目录 然后这个颜色靠这个设定 a:link {color:#110101;} /* 未访问链接*/a:visited {color:#00FF00;} /* 已访问链接 */a:hover {color:#FF00FF;} /* 鼠标移动到链接上 */a:active {color:#0000FF;} /* 鼠标点击时 */
新增的选择路径上传功能 当前路径下上传的文件就在这个文件夹下之前是放在默认存储路径下
如此现在算是正常上线了