【问题标题】:Javascript redirect to dynamic url issue in FlaskJavascript 重定向到 Flask 中的动态 url 问题
【发布时间】:2021-03-28 03:08:07
【问题描述】:

我有一个带有动态 url 的“拼图”路线,具体取决于可以正常工作的拼图编号:

@app.route("/puzzle/<puzzle_id>")
@login_required
def puzzle(puzzle_id):
    some sqlalchemy queries
    return render_template('puzzle.html', other variables)

我尝试使用尝试路由做同样的事情,但在 html 中使用 jquery javascript 进行重定向:

-(jquery function above here that worked fine without the redirect)-
}).done(function (response) {
    var attempt_num = response['attempt'];
    var urli = "{{ url_for('attempt', attempt_num=attempt_num) }}";
    console.log(urli)
    window.location.href = urli;

路线:

@app.route("/attempt/<attempt_num>")
@login_required
def attempt(attempt_num):
    some sqlalchemy queries to fill variables
    print("render attempt template with attempt = "+str(attempt.id),file=sys.stderr,flush=True)
    return render_template('attempt.html', other variables)

我的打印语句产生:

使用尝试 = 80 渲染尝试模板

并且烧瓶服务器显示:

127.0.0.1 - - [27/Mar/2021 19:50:27] "←[37mGET /attempt/80?redir=True HTTP/1.1←[0m" 200 - 127.0.0.1 - - [27/Mar/2021 19:50:27]“←[33mGET /attempt/HTTP/1.1←[0m”404 -

它看起来工作正常并尝试转到 /attempt/80,但随后它尝试转到 /attempt/ 而不是未定义的路由并导致 404。

【问题讨论】:

    标签: javascript python flask


    【解决方案1】:

    我添加了一个额外的控制台日志打印语句,发现 {{ url_for }} 块没有正确附加数字。我不明白为什么该部分不起作用,但我能够在重定向之前手动将数字附加到 url 字符串并且它起作用了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-29
      • 2011-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多