2021年3月27日星期六

Redirect to dynamic url issue in Flask

I have a 'puzzle' route with a dynamic url depending on the puzzle number that is working without issue:

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

I tried to do the same thing with an attempt route but using jquery javascript in the html to redirect:

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

for route:

@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)  

My print statement produces:

render attempt template with attempt = 80

and the flask server shows:

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 -

It looks like it is working correctly and trying to go to /attempt/80, but then it tries going to /attempt/ instead which isn't a defined route and causes a 404.

https://stackoverflow.com/questions/66838059/redirect-to-dynamic-url-issue-in-flask March 28, 2021 at 11:08AM

没有评论:

发表评论