Rewrite a URL with Flask

5 python url url-rewriting flask

Is it possible to rewrite a URL with Flask e.g. if a POST request is received via this route:

@app.route('/', methods=['GET','POST'])
def main():
    if request.method == 'POST':
        #TODO: rewrite url to something like '/message'
        return render_template('message.html')
    else:
        return render_template('index.html')
Run Code Online (Sandbox Code Playgroud)

I'm aware I can use a redirect and setup another route but I prefer to simply modify the url if possible.

hea*_*ad7 -1

我认为您可能对重定向行为感兴趣

from flask import Flask,redirect
Run Code Online (Sandbox Code Playgroud)

然后使用

redirect("http://www.example.com", code=302)
Run Code Online (Sandbox Code Playgroud)

请参阅:重定向到 Flask 中的 URL

  • 这会向服务器发出新的请求。有没有办法真正做到最初要求的事情? (6认同)