如何访问主干数据服务器sid

2024-04-28 12:53:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个主干模型,我称之为fetch。我有一个flask服务器,需要访问主干模型的id。我似乎无法在服务器上获得模型的id。如何访问flask代码中的entityId

BB.Politician = Backbone.Model.extend({
    defaults: {
        type: "Politician"
    },
    url: "/my_url_here"
});
var currentUser = new BB.Politician({"entityId": "1625"});
currentUser.fetch({
    //method: "POST",
    success: function(user){
        currentUserView.render();
    }
});

#Flask server code
@app.route('/my_url_here', methods=['GET', 'POST'])
def return_poitician():
    print request
    print request.args
    print request.values 

    #none of the above print statements are giving me the "entityId"
    return data

我还尝试在路由中添加id,但在执行fetch()时抛出了404错误:

@app.route('/my_url_here/<entityId>', methods=['GET', 'POST'])
def return_poitician(entityId):
    print entityId

Tags: 模型服务器idurlflaskreturnhererequest
1条回答
网友
1楼 · 发布于 2024-04-28 12:53:21
@app.route('/my_url_here/<entityId>', methods=['GET', 'POST'])

没有收到任何id,因为您没有发送任何。你知道吗

主干fetch使用模型的id字段来构造fetchurl,在您的示例中,我建议将entityId转换为id

BB.Politician = Backbone.Model.extend({
    defaults: {
        type: "Politician"
    },
    url: "/my_url_here"
});
var currentUser = new BB.Politician({"id": "1625"});

让主干构造GET,它看起来像:

"/my_url_here/" + this.get('id'); // this refers to model

变成了

"/my_url_here/1625"

Backbone.Model.url还接受函数作为值,这样您就可以定义自己的逻辑来构建URL。例如,如果必须保留entityId,则可以构建url,如下所示:

url: function () {
    return "/my_url_here" + this.get('entityId');
}

相关问题 更多 >