【问题标题】:Flask Application : add condition to check for environment in html template fileFlask Application:添加条件以检查 html 模板文件中的环境
【发布时间】:2021-10-11 00:47:58
【问题描述】:

我正在我的 Flask 应用程序中以 <iframe> 呈现应用程序。

本地主机链接是http://localhost:8000/dashboard2/,并将其推送到AWS Elastic Beanstalk,托管在:http://server-name.elasticbeanstalk.com/dashboard2

我的应用程序是用 python 编写的,dashboard.html jinja 模板呈现应用程序。

dashboard.html的内容:

{% extends "base.html" %}

{% block content %}

<div class="embed-responsive embed-responsive-16by9">

  <iframe loading="lazy" class="embed-responsive-item" src="http://localhost:8000/dashboard/" width="100%" height="600"></iframe>

</div>
{% endblock %}

问题是,我如何检查环境类型 os 并在 localhostproduction 上呈现应用程序?我需要在.html 模板中执行此操作。

有没有办法访问和打印jinja模板中的环境?

【问题讨论】:

    标签: python html flask amazon-elastic-beanstalk jinja2


    【解决方案1】:

    我会在运行应用程序时设置一个环境变量,指定应用程序应该以development 还是production 模式运行:

    FLASK_ENV=production flask run
    

    然后在您的模板文件中,您可以通过应用程序的config 访问环境并根据值呈现不同的内容:

    <div>
      {% if config["ENV"] == 'production' %}
        Production content...
      {% else %}
        Development content...
      {% endif %}
    </div>
    

    【讨论】:

    • 对于生产,我不“运行”应用程序,它部署在环境中。我会在脚本中的某个地方需要这个条件。
    • @kms 这种区别并不重要。只要以某种方式设置环境变量,这种方法就可以工作。我对elasticbeanstalk不太熟悉,但我发现了这个:docs.aws.amazon.com/elasticbeanstalk/latest/dg/…
    • 我明白了。我想我的意思是我无法在运行时/在生产中运行应用程序时设置环境。所以,我需要在脚本中的某个地方设置它并在 jinja 模板中检查它。
    猜你喜欢
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    相关资源
    最近更新 更多