Click here to Skip to main content
15,742,655 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone,

I am facing a hard time since I could not figure out how to redirect to the previous URL. When I tested request.referrer 10 days ago, it worked, but now it's not working anymore and instead, I am getting the same URL. I am creating a Google login. Here's the code:

@main.route("/post/<int:post_id>", methods=["GET", "POST"])
def post(post_id):
    post = Posts.query.filter_by(id=post_id).one()
    comments = post.comments

    if post == None:
        return ('Error')
    elif current_user.is_authenticated:
        return render_template("show_post0.html", post=post, comments=comments, username=current_user.user_name, userimage=current_user.user_photo)

    return render_template("show_post_login.html", post=post, comments=comments)

# redirect to previous url
def redirect_url(default=''):
    return request.args.get('next') or \
           request.referrer or \

def callback():
    # Get authorization code Google sent back to you
    code = request.args.get("code")

    # Find out what URL to hit to get tokens that allow you to ask for
    # things on behalf of a user
    google_provider_cfg = get_google_provider_cfg()
    token_endpoint = google_provider_cfg["token_endpoint"]

    # Prepare and send request to get tokens! Yay tokens!
    token_url, headers, body = client.prepare_token_request(
    token_response =

    # Parse the tokens!

    # Now that we have tokens (yay) let's find and hit URL
    # from Google that gives you user's profile information,
    # including their Google Profile Image and Email
    userinfo_endpoint = google_provider_cfg["userinfo_endpoint"]
    uri, headers, body = client.add_token(userinfo_endpoint)
    userinfo_response = requests.get(uri, headers=headers, data=body)

    # We want to make sure their email is verified.
    # The user authenticated with Google, authorized our
    # app, and now we've verified their email through Google!
    if userinfo_response.json().get("email_verified"):
        unique_id = userinfo_response.json()["sub"]
        users_email = userinfo_response.json()["email"]
        picture = userinfo_response.json()["picture"]
        users_name = userinfo_response.json()["given_name"]
        return "User email not available or not verified by Google.", 400

    # Create a user in our db with the information provided
    # by Google
    author = Author(
        google_id=unique_id, user_name=users_name, user_email=users_email, user_photo=picture

    user = Author.query.filter_by(google_id=unique_id).first()
    # Doesn't exist? Add to database
    if user:
        # Begin user session by logging the user in


    # Send user back to homepage

    print("##############", request.args.get('next')) #This produce None
    print ("#########", request.referrer) # this produce the link of the header, 
                                     not the previous page.

    return redirect(redirect_url())

I could redirect to the Post route directly but I want to use this method in more than one route.

Thanks in advance!

What I have tried:

I tried all the possible ways on the internet. though, when the login success, it refreshes the same URL instead of returning to the previous URL. I will be thankful if anyone can find a solution. Maybe I missed something. maybe it's a silly bug

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900