I have created a website with simple login using database. I want it so that when you visit the homepage, it prompts you to sign up/login; and when you log in, it redirects to the same homepage but the homepage must not show the prompt (hinting that you're already logged in) and show content that is only available to logged in users instead. So its route must still be "/". I've tried this:
<meta http-equiv="refresh" content="0; url={{ url_for('index', authenticated=1) }}">
and in my Python file:
build = Flask(__name__)
@build.route('/')
def index(authenticated=0):
if authenticated == 0:
return render_template("index.html")
elif authenticated == 1:
return render_template("main.html")
Why did I want it to be that way? Well.. I don't want new users to visit that exclusive link. So unless there's a workaround where the exclusive link detects if the new user is not logged in, I don't know any other way to accomplish this
CodePudding user response:
You can do this by creating two different routes also.
Just for example, here the admin
route is for logged in users and home
is for normal users. If the user is authenticated it redirects to the admin
func.
@app.route("/login", methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('admin'))
else:
return redirect(url_for('home'))
@app.route("/admin")
def admin():
return render_template('admin_home.html')
@app.route("/home")
def home():
return render_template('home.html')
CodePudding user response:
You must use redirects like this:
from flask import Flask, redirect, render_template
app = Flask(__name__)
@app.route("/")
def index():
if authenticated == 0:
return redirect('/login')
return render_template("index.html")
@app.route("/login")
def login():
if authenticated == 1:
return redirect('/')
return render_template("index.html")