display database field in html python flask multiple buttons
Step 1: Create a Flask App
from flask import Flask, render_template
app = Flask(__name__)
# Your database related code here
@app.route('/')
def index():
# Fetch data from the database
data = # Your database query to fetch the required field
return render_template('index.html', data=data)
if __name__ == '__main__':
app.run()
Step 2: Create HTML Template (index.html)
<!DOCTYPE html>
<html>
<head>
<title>Display Database Field</title>
</head>
<body>
<h1>Database Field</h1>
<p>{{ data }}</p>
<form action="/process_data" method="POST">
<button type="submit" name="action" value="button1">Button 1</button>
<button type="submit" name="action" value="button2">Button 2</button>
</form>
</body>
</html>
Step 3: Process Button Click in Flask
from flask import request
@app.route('/process_data', methods=['POST'])
def process_data():
action = request.form['action']
# Process the button click based on the value
if action == 'button1':
# Perform action for button 1
elif action == 'button2':
# Perform action for button 2
return 'Action processed'
This code sets up a Flask app to display a database field in an HTML template and handles button clicks to process the data accordingly.