使用谷歌应用引擎插入CSS或基于cookie执行JS

问题描述 投票:0回答:1

我想根据cookie是否存在显示一个简单的弹出式横幅。这是我的python代码

consent_cookie_str = self.request.cookies.get('consent')
        if not consent_cookie_str:
            // show banner here

如果cookie不存在,显示横幅的最佳方式是什么?将参数传递到我的jinja模板中?

谢谢

python google-app-engine cookies
1个回答
0
投票

您可以创建如下模板:

<html>
  <body>
    <h1>Welcome!</h1>
    <script type="text/javascript">
      $(document).ready(function() {
      {% if show_alert %}
        alert('You have not given consent!');
      {% endif %}
      });
    </script>
  </body>
</html>

并从这样的处理程序中调用它(使用webapp2):

consent_cookie_str = self.request.cookies.get('consent')
show_alert = consent_cookie_str is None
template_values = dict(show_alert=show_alert)
template = JINJA.get_template("page.html")
self.write_html(template.render(template_values))

如果您正在使用Flask或其他东西,应该直接修改它以适合您。

© www.soinside.com 2019 - 2024. All rights reserved.