0

Hy I'm new to flask and trying to get my first site up, I have been trying this for a couple of days now with little to no progress, Here is some code:

from flask import Flask, render_template, url_for, redirect, request, session

app = Flask(__name__)

@app.route('/', methods=["POST", "GET"])
def purchase():
    try:
        
        if request.method == "POST":
            email = request.form["email"]
            # Query the data base to find the exact amount and name of buyer

            session["user"] = email
            return redirect(url_for("proceed"))
            
        else:
            return render_template("index.html",logged_in=True)
            
    except Exception as e:
        return(str(e))


@app.route("/confirm")
def proceed():
    # Get information and create another form
    if "user" in session:
        return f"""<h1>{session["user"]}</h1>"""
    else:
        return f"""<h1>No email found</h1>"""
if __name__ == "__main__":
    
    app.run()

The first purchase function works just fine, the problem appears when the user get redirected to proceed. The '/confirm' tag is added to the url of the site and the site goes to a **404 Not Found ** page. I have had problems changing pages with cPanel. What would be the best way to send the user of my site to a totally different page where i can add an HTML file of my choosing.

2 Answers2

2

Firstly, you need to change the redirect(url_for('proceed')) to redirect(url_for('confirm'))

And the code should look like this:

from flask import Flask, render_template, url_for, redirect, request, session

app = Flask(__name__)

@app.route('/', methods=["POST", "GET"])
def purchase():
    try:
        
        if request.method == "POST":
            email = request.form["email"]
            # Query the data base to find the exact amount and name of buyer

            session["user"] = email
            return redirect(url_for("cofirm"))
            
        else:
            return render_template("index.html",logged_in=True)
            
    except Exception as e:
        return(str(e))


@app.route("/confirm")
def proceed():
    # Get information and create another form
    if "user" in session:
        return f"""<h1>{session["user"]}</h1>"""
    else:
        return f"""<h1>No email found</h1>"""
if __name__ == "__main__":
    
    app.run()

And since you are hosting on Cpanel, change your .htaccess file to add the following so apache redirect all the not-found URL to your application

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [L]
orb
  • 43
  • 1
  • 10
2

Add the following rules to your .htaccess in the directory of your application URL

RewriteEngine on
RewriteRule ^http://%{HTTP_HOST}%{REQUEST_URI} [END,NE]
President James K. Polk
  • 40,516
  • 21
  • 95
  • 125