PywebLesson02
程序员文章站
2022-06-13 13:19:45
...
PywebLesson02
文章目录
上节课的问题
上一次出现的问题,由于我创建了新的项目,然后原来的那个项目选择了disconnect,而disconected而不是选择terminate,dicconected是一种伪断开,只是在pycharm这里中止了,但是python解释器依旧在运行这个服务。因此。当时一直在运行原来的程序
解决方法https://blog.csdn.net/weixin_41733260/article/details/100532091
Web表单
原生实现form
form表格代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>表格</title>
</head>
<body>
<form method="post">
<label>姓名</label><input type="text" name="username"><br>
<label>学号</label><input type="text" name="account"><br>
<label>密码</label><input type="password" name="password"><br>
<input type="submit" value="提交"><br>
</form>
</body>
</html>
# 1.登录,需要get和post,判断用get还是post
# 2.获取请求参数
# 3.获取密码,账号
@app.route('/form', methods=['POST', 'GET'])
def form():
# request请求对象->获取数据
if request.method == 'POST':
# 获取请求参数
username = request.form.get('username')
account = request.form.get('account')
password = request.form.get('password')
print(username + '----' + str(account) + '----' + str(password))
return render_template('form.html', username=username, account=account, password=password)
return render_template('form.html')
SQLAlchemy拓展
无需写具体的SQL语句,框架通过映射将sql代码实现
select ( ),( ),( ) form (staff) where ( ) = ( )
delete from ****
update ( ) from ( )
insert ( ) into
安装flask-sqlalchemy,Flask-Mysqldb,PyMysql
安装成功,检查
连接数据库
from flask import Flask, render_template, request, flash
from flask_sqlalchemy import SQLAlchemy
import pymysql
app = Flask(__name__)
app.secret_key = 'gdpujf'
#打开wamp
#配置路径,//账号密码@127.0.0.1:3306/数据库名称
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:@127.0.0.1:3306/gdpujf'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
创建模型
class Staff(db.Model):
# 定义表和字段
__tablename__ = 'staff'
# 定义字段
name = db.Column(db.String(10), primary_key=True)
account = db.Column(db.String(20))
password = db.Column(db.String(100))
添加数据
staff = Staff(name=username, account=account, password=password)
db.session.add(staff)
db.session.commit()
添加多个数据
staff1 = Staff(name=username, account=account, password=password)
staff2 = Staff(name=username, account=account, password=password)
db.session.add_all([staff,staff2])
db.session.commit()
删除
注意:增删改都需要commit操作
查询
#查询全部
Staff.query.all()
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
import pymysql
app = Flask(__name__)
# app.secret_key = 'gdpujf'
#配置路径
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:aaa@qq.com:3306/gdpujf'
#防止报错
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# 创建模型,继承db.Model
class Staff(db.Model):
# 定义表和字段
__tablename__ = 'staff'
# 定义字段
name = db.Column(db.String(10), primary_key=True)
account = db.Column(db.String(20))
password = db.Column(db.String(100))
@app.route('/')
def hello_world():
return '789'
# 1.登录,需要get和post,判断用get还是post
# 2.获取请求参数
# 3.获取密码,账号
@app.route('/form', methods=['POST', 'GET'])
def form():
#request请求对象->获取数据
if request.method == 'POST':
# 获取请求参数
username = request.form.get('username')
account = request.form.get('account')
password = request.form.get('password')
print(username + '----' + str(account) + '----' + str(password))
staff = Staff(name=username, account=account, password=password)
db.session.add(staff)
db.session.commit()
return render_template('form.html', username=username, account=account, password=password)
#return render_template('form.html', username=username, account=account, password=password)
# # 3.表单验证
# if not all([username, password]):
# print('参数不完整')
# else:
# staff = Staff(name=username, account=account, password=password)
# db.session.add(staff)
# db.session.commit()
# return render_template('form.html', staff=staff)
return render_template('form.html')
@app.route('/insert')
def insert():
staff = Staff(name='测试插入功能', account='123456', password='123456')
db.session.add(staff)
db.session.commit()
return '插入成功'
#增、删、改
@app.route('/getStaff')
def getStaff():
staffs = Staff.query.all()
return render_template('getdata.html', staffs=staffs)
# @app.route('/getdata')
# @app.route('/getdata/<username>')
# def getdata(username):
# #staffs = Staff.query.all()
# staffs = Staff.query.filter_by(name=username).first_or_404()
# print(staffs.account+'----'+staffs.password)
# return render_template('getdata.html')
#
#
# @app.route('/delete/<username>')
# def delete(username):
# staff = Staff.query.filter_by(name=username).first_or_404()
# db.session.delete(staff)
# db.session.commit()
# return '删除成功'
if __name__ == '__main__':
app.run(debug=True)
遍历数据库
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{#{% for staff in staffs %}#}
{# {{ staff.name }} {{ staff.account }} {{ staff.password }}<br>#}
{#{% endfor %}#}
{##}
{#<table border="1">#}
{# <th>姓名</th>#}
{# <th>学号</th>#}
{# <th>密码</th>#}
{# </tr>#}
{#{% for staff in staffs %}#}
{# <tr>#}
{# <td>{{ staff.name }}</td>#}
{# <td>{{ staff.account }}</td>#}
{# <td>{{ staff.password }}</td>#}
{# </tr>#}
{#{% endfor %}#}
{##}
{#</table>#}
<table border="1">
<th>姓名</th>
<th>学号</th>
<th>密码</th>
{# 遍历数据 #}
{% for staff in staffs %}
<tr>
<td>{{ staff.name }}</td>
<td>{{ staff.account }}</td>
<td>{{ staff.password }}</td>
</tr>
{% endfor %}
</table>
获取成功
</body>
</html>
上一篇: (Filter)过滤器的使用
下一篇: Filter 的基本使用
推荐阅读