[英]How to ignore a custom attribute when rendering flask render_template?
我有一些 HTML 模板,例如:
<span data-test-id="fullName">{{ user.full_name }}</span>
↓ 在 dev/qa 環境中
<span data-test-id="fullName">Homer Simpson</span>
我想在生產環境中忽略自定義屬性data-test-id
。
<span data-test-id="fullName">{{ user.full_name }}</span>
↓ 在 dev/qa 環境中
<span>Homer Simpson</span>
是否可以?
您可以將FLASK_ENV
值用作開關並將其作為kwarg
傳遞到您的render_template
調用中。 您需要查看Jinja2以獲得更強大的功能。
Flask 應用程序
import os
...
@app.route('/')
def your_route():
...
environment = os.environ.get("FLASK_ENV", 'dev')
return render_template('your.html', environment=environment)
HTML
<span {% if environment != 'production'%} data-test-id="fullName" {% endif %}>{{ user.full_name }}</span>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.