如何在JavaScript中访问Flask配置? [英] How to access flask config in javascript?

查看:103
本文介绍了如何在JavaScript中访问Flask配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的flask应用程序中需要javascript中的配置变量。当前,我通过以下方式访问配置变量:

I need the config variables in javascript in my flask application. Currently, I access the config variables via:

@app.route('/api/getconfigs/')
def get_config_variable():
    return config['VARIABLENAME']

这是唯一的方法获得烧瓶配置在JavaScript?最佳做法是什么?

Is this the only way to get the flask config in javascript? What would be the best practice to do this?

推荐答案

如果您需要将该值作为API调用的一部分,那么可以,从端点发送值是正确的。如果要渲染模板,则可以在发送给客户端的JavaScript中渲染值。

If you need the value as part of an API call, then yes, sending the value from an endpoint is correct. If you're rendering templates, then you can render the values in the JavaScript sent to the client.

Flask将其配置注入模板上下文中。在模板中, config ['key'] config.key 将访问配置中的任何值。由于您将其呈现为JavaScript,因此请使用 tojson 过滤器呈现有效值。

Flask injects its config into the template context. Within a template, config['key'] or config.key will access any value in the config. Since you're rendering it as JavaScript, use the tojson filter to render valid values.

var debug = {{ config['DEBUG']|tojson }};

这篇关于如何在JavaScript中访问Flask配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆