To set up a button on the homepage that redirects to the first page, you can make the following changes to your code:

  1. In the index.html file, add the following code for the button:
<form action="{{ url_for('home') }}" method="GET">
    <button type="submit">Go to First Page</button>
</form>
  1. In the app.py file, modify the home() function to accept both GET and POST requests:
@app.route('/', methods=['GET', 'POST'])
def home():
    if request.method == 'POST':
        return redirect(url_for('first_page'))
    
    return render_template('index.html')
  1. Add a new route and function for the first page:
@app.route('/first-page')
def first_page():
    return render_template('first_page.html')
  1. Create a first_page.html file in the templates folder and add the content for the first page.

Now, when the "Go to First Page" button is clicked on the homepage, it will redirect to the first page

import mysqlconnectorfrom flask import Flask render_template request redirect url_forimport timeapp = Flask__name__# Connect to MySQL databasemydb = mysqlconnectorconnect host=localhost user=roo

原文地址: https://www.cveoy.top/t/topic/hWDj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录