The content of this article is about the code example of displaying database information on a python web page based on flask_sqlalchemy. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Web page displays database information
Use the flask_sqlalchemy we just learned to display the data in the database table on the web page.
Before starting to run the program, make sure that the operations of creating tables and users have been executed in the database. For details, see Link description.
# 模板文件templates/list.html {% extends 'base.html' %} {% block title %} 显示 {% endblock %} {% block newcontent %}
用户编号 | 用户名称 | 用户密码 | 用户创建时间 | 用户会员类型 |
---|---|---|---|---|
{{ user.id }} | {{ user.name }} | {{ user.passwd }} | {{ user.add_time }} | {{ user.role.name }} |
# 数据库操作文件zaj_sql_models.py from datetime import datetime from flask_bootstrap import Bootstrap from flask_wtf import FlaskForm from flask_sqlalchemy import SQLAlchemy from flask import Flask import pymysql from sqlalchemy import desc app = Flask(__name__) db = SQLAlchemy(app) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:sheen@localhost/zaj_sql' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True bootstrap = Bootstrap(app) # class Student(db.Model): # __tablename__ = 'students' # sid = db.Column(db.SMALLINT,primary_key=True) # sname = db.Column(db.String(50)) # sage = db.Column(db.Integer) class User(db.Model): id = db.Column(db.Integer,autoincrement=True,primary_key=True) name = db.Column(db.String(50),unique=True) passwd = db.Column(db.String(100)) add_time = db.Column(db.DATETIME,default=datetime.now()) gender = db.Column(db.BOOLEAN,default=True) role_id = db.Column(db.INTEGER,db.ForeignKey('role.id')) def __repr__(self): return '<user:>' %(self.name) class Role(db.Model): id = db.Column(db.INTEGER,autoincrement=True,primary_key=True) name = db.Column(db.String(50),unique=True) users = db.relationship('User',backref='role') # 给Role模型添加users属性 # backref 是定义反向引用,可以通过User.role访问User里面的数据 def __repr__(self): return '<role:>' % (self.name) if __name__ =='__main__': # 1. 创建数据库表 db.drop_all() db.create_all() # 2. 创建role数据库表数据 role_1 = Role(name='超级会员') role_2 = Role(name='普通会员') db.session.add(role_1) db.session.add(role_2) db.session.commit() # # # 3. 添加user表内数据,100个用户,50个为超级会员,50个为普通会员 for i in range(1,13): if i%2 == 0: u = User(name='sheen'+str(i),passwd='sheen',role_id=1) db.session.add(u) else: u = User(name='star'+str(i),passwd='star',role_id=2) db.session.add(u) db.session.commit()</role:></user:>
#主程序 from flask import Flask,render_template from zaj_sql_models import app from zaj_sql_models import User @app.route('/') def index(): return render_template('index.html') @app.route('/list/') def list(): users = User.query.all() return render_template('list.html',users=users) if __name__ == '__main__': app.run()
The above is the detailed content of Python code example for displaying database information on a web page based on flask_sqlalchemy. For more information, please follow other related articles on the PHP Chinese website!