import mysqlconnectorfrom flask import Flask render_template request redirect url_forimport timeapp = Flask__name__# Connect to MySQL databasemydb = mysqlconnectorconnect host=localhost user=roo
To set up a button on the homepage that redirects to the first page, you can make the following changes to your code:
- In the
index.htmlfile, add the following code for the button:
<form action="{{ url_for('home') }}" method="GET">
<button type="submit">Go to First Page</button>
</form>
- In the
app.pyfile, modify thehome()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')
- Add a new route and function for the first page:
@app.route('/first-page')
def first_page():
return render_template('first_page.html')
- Create a
first_page.htmlfile 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
原文地址: https://www.cveoy.top/t/topic/hWDj 著作权归作者所有。请勿转载和采集!