Answers for "how to redirect to another page in python"

0

how to redirect to another page in python

from flask import Flask, redirect, request

app = Flask(__name__)

from urllib.parse import urlparse, urlunparse

FROM_DOMAIN = "yourusername.pythonanywhere.com"
TO_DOMAIN = "www.yourdomain.com"

@app.before_request
def redirect_to_new_domain():
    urlparts = urlparse(request.url)
    if urlparts.netloc == FROM_DOMAIN:
        urlparts_list = list(urlparts)
        urlparts_list[1] = TO_DOMAIN
        return redirect(urlunparse(urlparts_list), code=301)
Posted by: Guest on July-11-2020

Code answers related to "how to redirect to another page in python"

Python Answers by Framework

Browse Popular Code Answers by Language