我有一个大型网站,在各个 html 页面中包含数百个 href=".html、href="css/、href="img/ 和 src="js/ 引用。我已将所有 css、js、img、声音等文件夹放在“static”文件夹中,并将所有 html 文件放在“模板文件夹”中。
我不想浏览所有 html 文件并使用 Flask 的“url_for”函数手动编辑每个 href。
有没有一种方法可以让我保留现有的 html 文件并告诉 Flask 使用这些 html 文件中定义的 css/、js/ 和图像路径?
现在以下代码会导致一堆 404 错误
import os
from flask import Flask, render_template
PROJECT_PATH = os.path.dirname(os.path.realpath(__file__))
app = Flask(__name__,
template_folder=os.path.join(PROJECT_PATH, 'templates'),
static_folder=os.path.join(PROJECT_PATH, 'static')
)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)
Run Code Online (Sandbox Code Playgroud)
好的,根据 Martijn 的脚本,我使用以下代码运行了我的网站。但是,为了捕获 index.html 文件中的许多 href=someHTMLfile.html 链接,我添加了 @app.route('/')。我想知道是否有更好的方法来做到这一点?
from flask import Flask, render_template, safe_join, send_from_directory
app = Flask(__name__)
@app.route('/<any(css, js, img, fonts, …
Run Code Online (Sandbox Code Playgroud) 我有一个客户端api,它发出以下GET请求:
"GET /tasks/5fe7eabd-842e-40d2-849e-409655e0891d?{%22task%22:%22hello%22,%22url%22:%22/tasks/5fe7eabd-842e-40d2-849e-409655e0891d%22}&_=1411772296171 HTTP/1.1" 200 -
在做
task = request.args
print task
Run Code Online (Sandbox Code Playgroud)
显示它是
ImmutableMultiDict([('{"task":"hello","url":"/tasks/5fe7eabd-842e-40d2-849e-409655e0891d"}', u''), ('_', u'1411772296171')])
要获取“任务”值,请执行以下操作:
request.args.getlist('task')
Run Code Online (Sandbox Code Playgroud)
我得到[]
代替hello
。关于如何从中打招呼的任何想法?
如何在 marshmallow_mongoengine 中取消引用 ReferenceFields?例如,dump_data = author_schema.dump(author).data
results in'5578726b7a58012298a5a7e2'
而不是更有用的 response {title='Fight Club', author=author}
。
from marshmallow_mongoengine import ModelSchema
class AuthorSchema(ModelSchema):
class Meta:
model = Author
class BookSchema(ModelSchema):
class Meta:
model = Book
author_schema = AuthorSchema()
author = Author(name='Chuck Paluhniuk').save()
book = Book(title='Fight Club', author=author).save()
dump_data = author_schema.dump(author).data
# {'id': 1, 'name': 'Chuck Paluhniuk', 'books': ['5578726b7a58012298a5a7e2']}
author_schema.load(dump_data).data
# <Author(name='Chuck Paluhniuk')>
Run Code Online (Sandbox Code Playgroud)