from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
user_authenticated = True # Replace with your authentication logic
if user_authenticated:
message = 'Welcome, User!'
else:
message = 'Please log in to access the content.'
return render_template('index.html', message=message)
if __name__ == '__main__':
app.run(debug=True)
- Import the
Flask
class and the render_template
function. - Create an instance of the
Flask
class and name it app
. - Define a route for the root URL ('/') using the
@app.route
decorator. - Create a function named
index
to handle requests to the root URL. - Set a boolean variable
user_authenticated
based on your authentication logic. - Use an if statement to check if the user is authenticated.
- If authenticated, set the
message
variable to 'Welcome, User!'; otherwise, set it to 'Please log in to access the content.' - Return the rendered template 'index.html' with the
message
variable as a parameter. - If the script is executed directly (not imported), start the Flask development server with debugging enabled.