利用flask写出基本逻辑----------------------------------------------

from flask import Flask,render_template,request,session,redirect,url_for,flash,jsonify
import aaa
app = Flask(name)
app.secret_key = ‘1810’
@app.route(’/’,methods=[‘GET’,‘POST’])
def login():
if request.method == ‘POST’:
username = request.form.get(‘username’)
password = request.form.get(‘password’)
if password == ''or username == ‘’:
flash(‘不能为空’)
else:
users = aaa.get1(
‘select * from customer where username = “{}” and password="{}"’.format(username,
password))
if len(users) > 0:
session[‘username’] = username
print(‘登录成功’)
return redirect(url_for(‘goods’))
else:
flash(‘登录失败!!!’)

return render_template('Login.html')

@app.route(’/goods’,methods=[‘GET’,‘POST’])
def goods():
if request.form.get(‘cart1’) is not None:
return redirect(url_for(‘cart’))
else:
if session.get(‘username’) is None:
return redirect(url_for(‘login’))
else:
result = aaa.get1(‘select * from goods’)
alist = []
for i in result:
dic2 = {}
dic2[‘id’] = i[0]
dic2[‘name’] = i[1]
dic2[‘price’] = str(i[2])
dic2[‘inventory’] = i[3]
alist.append(dic2)
return render_template(‘temp2.html’, p_goods=alist)
@app.route(’/register’,methods=[‘GET’,‘POST’])
def register():
if request.method == ‘POST’:
username = request.form.get(‘username’)
password = request.form.get(‘password’)
repassword = request.form.get(‘repassword’)
if not all([password,username,repassword]):
flash(‘不能为空’)
else:
user = aaa.get1(‘select * from customer where username = “{}”’.format(username))
if len(user) > 0:
flash(‘用户名已经存在!!!’)
else:
if password != repassword:
flash(‘两次密码输入不一致’)
else:
aaa.into1(‘insert into customer values(0,"’+username+’","’+password+’")’)
return redirect(url_for(‘login’))
return render_template(‘register.html’)
@app.route(’/add_goods’,methods=[‘GET’,‘POST’])
def add_goods():
id = request.json[‘id’]
name = request.json[‘name’]
username = session.get(‘username’)
c_id = aaa.get1(‘select id from customer where username = “{}”’.format(username))
cid = c_id[0][0]
print(cid)
res = aaa.get1(‘select * from cart where gid = “{}” and cid = {}’.format(id,cid))
if len(res) > 0:
aaa.into1(‘update cart set count = count+1 where gid="{}" and cid = “{}”’.format(id,cid))
else:
aaa.into1(‘insert into cart values(0,{},{},1)’.format(cid,int(id)))
return jsonify(request.json)
@app.route(’/cart’,methods=[‘GET’,‘POST’])
def cart():
if session.get(‘username’) is not None:
p_goods = aaa.get1(‘select customer.username,goods.id,goods.name,goods.price,count from customer,goods,cart where cart.gid = goods.id and cart.cid = customer.id and username = “{}”’.format(session.get(‘username’)))
print(p_goods)
alist = []
for i in p_goods:
set1 = {}
set1[‘username’] = i[0]
set1[‘gid’] = str(i[1])
set1[‘gname’] = i[2]
set1[‘price’] = str(i[3])
set1[‘count’] = str(i[4])
alist.append(set1)
# print(alist)
return render_template(‘cart.html’,pp_goods = alist)
if name == ‘main’:
app.config[‘JSON_AS_ASCII’] = False
app.run(debug=True)
html展示-----------------------------------------------------


登录

{%for message in get_flashed_messages()%} {{message}} {%endfor%}

用 户 名:
密 码:
确认密码:
注册 重置

{%for message in get_flashed_messages()%} {{message}} {%endfor%}

{% for item in pp_goods %} {% endfor %}
{% for item in p_goods %} {% endfor %}
进入购物车
03-11 07:01