![](/img/trans.png)
[英]werkzeug.routing.BuildError: Could not build url for endpoint 'profile'. Did you mean 'home' instead? with Flask - Python
[英]Flask python - werkzeug.routing.BuildError: Could not build url for endpoint
我從網站下載 output 文件時遇到問題。 我正在使用 Flask 上傳文件,處理它們,生成 output 並下載文件。
您可以在下面看到我收到的錯誤:
似乎我沒有傳遞文件名,但我不確定如何修復它。 任何幫助或建議將不勝感激。
flask.py 中與此問題相關的函數
UPLOAD_FOLDER = os.path.dirname(os.path.abspath(__file__)) + '/uploads/'
@app.route('/compare_content', methods=['GET', 'POST'])
def compare():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
files = request.files.getlist('file')
files = [read_xlsx_binary(file) for file in files]
df_old, df_new = files
output_filename = run_compare_dfs(UPLOAD_FOLDER, df_old, df_new)
return redirect(url_for('download_comparison', filename=request.args.get(output_filename)))
return render_template('index.html', username=session['username'])
def read_xlsx_binary(file):
filename = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(filename)
df = pd.read_excel(filename, engine="openpyxl")
# os.unlink(filename)
return df
# Allow user to download the output that was generated by run_compare_dfs() function
@app.route('/uploads')
def download_comparison(filename):
return send_from_directory(UPLOAD_FOLDER, request.args.get('filename'), as_attachment=True)
比較.py
def run_compare_dfs(upload_folder, df_old=None, df_new=None):
if df_old is None or df_new is None:
path_OLD = 'old.xlsx'
path_NEW = 'new.xlsx'
df_old = pd.read_excel(path_OLD, engine="openpyxl")
df_new = pd.read_excel(path_NEW, engine="openpyxl")
final_df = compare_dfs(df_old, df_new)
output_filename = to_formatted_excel(upload_folder, final_df)
return output_filename
您可以通過更改以下代碼來嘗試嗎 -
我沒有測試過它,但它應該對你有用。
from flask import request
@app.route('/uploads')
def download_comparison():
return send_from_directory(UPLOAD_FOLDER, request.args.get('filename'), as_attachment=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.