我们的服务器上运行了一个Flask应用程序(xxxx.edu.au:5000)。但是,我们设置了xxxx.edu.au/getseq代理,将请求转发到xxxx.edu.au:5000

不幸的是,现在在浏览器中,我们得到Loading failed for the <script> with source “https://xxxx.edu.au/static/vehicle.js”

这是flask应用程序的结构:

flask
├── getseq.py
├── static
│   └── vehicle.js
└── templates
    └── example.html


该烧瓶应用程序写在这里:

$ cat getseq.py
from flask import Flask, render_template, request
from wtforms import Form, RadioField, TextAreaField
from wtforms.widgets import TextArea

SECRET_KEY = 'development'

app = Flask(__name__)
app.config.from_object(__name__)
...

@app.route("/getseq/<mrna_id>", methods=['post', 'get'])
def get_sequences(mrna_id):
    ...
    return render_template('example.html', form=form)

@app.route("/getseq/health", methods=['get'])
def health():
    response = app.response_class(
        status=200,
        mimetype='text/html'
    )
    return response

if __name__ == '__main__':
    print("starting...")
    app.run(host='0.0.0.0',port=5000,debug=True)


路径vehicle.js在这里定义:

$ cat templates/example.html
<script type="text/javascript" src="{{url_for('static', filename='vehicle.js')}}"></script>
...


如何更改url_for或必须更改getseq.py

先感谢您,

最佳答案

如果不重复您的问题,很难说出是什么原因导致您的静态资产发生故障。作为一种解决方法,您可以在服务器上读取vehicle.js,对其进行base64编码,将其传递到模板上下文中,并使用以下命令呈现脚本标签:

<script type="text/javascript" src="data:text/javascript;base64,{{ base64_encoded_data }}"></script>


编辑
在您的视图处理程序中:

import base64


@app.route("/getseq/<mrna_id>", methods=['post', 'get'])
def get_sequences(mrna_id):
    with open('static/vehicle.js', 'rb') as f:
        base64_encoded_data = base64.b64encode(f.read())
    return render_template('example.html', form=form, base64_encoded_data=base64_encoded_data)

关于python - url_for为静态文件夹创建了错误的路径,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58541222/

10-11 20:29