Jinja2:Flask的url_for()与Knockout的attr绑定结合 [英] Jinja2: Flask's url_for() combined with Knockout's attr binding

查看:124
本文介绍了Jinja2:Flask的url_for()与Knockout的attr绑定结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Jinja2作为模板引擎的Web应用程序中一起使用Flask和Knockout.在我的Flask应用程序中,我有两条路径:/accounts//accounts/<int:acc_id>/,看起来像这样:

I'm using Flask and Knockout together in a web application with Jinja2 as the templating engine. In my Flask application I have two routes: /accounts/ and /accounts/<int:acc_id>/, which look something like this:

@app.route("/accounts/")
@login_required
def accounts():
    return render_template("accounts.html")


@app.route("/accounts/<int:acc_id>/")
@login_required
def account(acc_id):
    return render_template("account.html",
        account_id=acc_id)

/accounts/路由将加载带有Knockout视图模型的HTML页面.然后,视图模型将AJAX的帐户列表加载到observableArray中,并呈现如下内容:

The /accounts/ route loads an HTML page with a Knockout view model. The view model then AJAX loads a list of accounts into an observableArray and renders something like this:

<!-- ko foreach: accounts -->
    <span data-bind="text: name></span>
<!-- /ko -->

/accounts/<int:acc_id>/路由显示有关单个帐户的信息.

The /accounts/<int:acc_id>/ route displays information about a single account.

我想做的是使用url_for()在上面的HTML代码段中的每个帐户上创建一个href属性,并将该帐户的id作为参数传递.我无法使用Knockout模型中的参数对url_for()进行Python调用.

What I'm trying to do is create an href attribute on each account in the HTML snippet above using url_for(), passing the account's id as a parameter. I'm having trouble making the Python call to url_for() with a parameter from the Knockout model.

到目前为止,我已经尝试了几种变体:

A couple of variations I've tried so far:

将ID连接到url_for函数调用中:

<a data-bind="attr: { href: '{{ url_for('account', acc_id=' + id() + ' }}'}">
    <span data-bind="text: name"></span>
</a>

由于它将对id()的调用读取为字符串文字,因此会生成ValueError: invalid literal for int() with base 10: '+ id() +'.

This produces a ValueError: invalid literal for int() with base 10: '+ id() +' since it reads the call to id() as a string literal.

在视图模型中创建一个函数,该函数返回url_for字符串:

self.accountUrl = function(account_id) {
    return '{{ url_for("admin.account", acc_id=' + account_id + ') }}';
};

并这样称呼它:

<a data-bind="attr: { href: $root.accountUrl(id()) }">
    <span data-bind="text: name"></span>
</a>

这导致href属性被设置为从accountUrl()返回的字符串文字,即HTML看起来像这样:

This results in the href attribute being set as the string literal returned from accountUrl(), i.e. the HTML looks like this:

<a href='{{ url_for("account", acc_id=1) }}'>

我觉得这里有些东西我必须忽略.有什么想法吗?

I feel like there's something I must be overlooking here. Any ideas?

推荐答案

在生成HTML时,您的python代码(url_for)在服务器端运行,而在呈现页面时,Knockout代码在浏览器中运行.因此,您不能将变量从Knockout传递到python url_for方法.

Your python code (url_for) runs on the server side when the HTML is generated but the Knockout code runs your in the browser when the page is rendered. So you cannot pass variables from Knockout into to the python url_for method.

您可以做的是仅生成/accounts/并使用KO构建URL的其余部分:

What you can do is to generate only the /accounts/ and build the rest of the URL with KO:

<a data-bind="attr: { href: '{{ url_for('account') }}' + '/' + id() }">
    <span data-bind="text: name"></span>
</a>

这篇关于Jinja2:Flask的url_for()与Knockout的attr绑定结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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