我试图从Flask终结点制作一个PDF文档,该终结点基本上是从MySql数据库中检索值并将其显示在HTML页面上。我尝试了jsPDF,但无法获得结果
这是我的Python代码:
from flask import Flask, render_template, request
from flask_mysqldb import MySQL
app = Flask(__name__)
# configure db
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'username'
app.config['MYSQL_PASSWORD'] = 'dbpassword'
app.config['MYSQL_DB'] = 'flaskapp'
mysql = MySQL(app)
@app.route('/', methods=['GET', 'POST'])
def alpha():
if request.method == 'POST':
# Fetch the form data
userDetails = request.form
name = userDetails['name']
email = userDetails['email']
cur = mysql.connection.cursor()
cur.execute("INSERT INTO users(name,email) VALUES(%s,%s)", (name, email))
mysql.connection.autocommit(on=True)
cur.close()
return 'success'
return render_template('index.html')
@app.route('/users')
def users():
cur = mysql.connection.cursor()
resultValue = cur.execute("SELECT * from users")
if resultValue > 0:
userDetails = cur.fetchall()
return render_template('users.html', userDetails=userDetails)
@app.route('/showpdf')
def showpdf():
return render_template('showpdf.html')
if __name__ == '__main__':
app.run()
以下是html页面:users.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Display Records From DB</title>
</head>
<body>
<style>
th {
background-color: orange;
}
</style>
<table border="1">
<tr>
<th>
Name
</th>
<th>
Email Id
</th>
</tr>
{% for users in userDetails %}
<tr>
<td>{{users[0]}}</td>
<td>{{users[1]}}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
千万里不及你
慕标5832272
相关分类