{% block title %}{% endblock %}base {% block head %}{% endblock %} {% block main %}{% endblock %}
from flask import Flask,request,render_template,redirect,url_for,sessionfrom flask_sqlalchemy import SQLAlchemyimport configfrom functools import wrapsfrom datetime import datetimeapp = Flask(__name__)app.config.from_object(config)db=SQLAlchemy(app)class User(db.Model): __tablename__='user' id=db.Column(db.Integer,primary_key=True,autoincrement=True) username = db.Column(db.String(20), nullable=False) password = db.Column(db.String(20), nullable=False) nickname = db.Column(db.String(50))#db.create_all()#增加数据#user=User(username = 'shanshan1',password = '111111')#db.session.add(user)#db.session.commit()#查询数据#user=User.query.filter(user.username == 'shanshan1').first()#print(user.id,user.username,user.password)#修改数据#user=User.query.filter(user.username == 'shanshan3').first()#user.password = '0000'#db.session.commit()#删除数据#user=User.query.filter(user.username == 'shanshan3').first()#db.session.delete(user)#db.session.commit()@app.route('/')def index(): return render_template('shouye.html')@app.route( '/manhua')def manhua(): return render_template('manhua.html')@app.route('/login',methods=['GET','POST'])def login(): if request.method =='GET': return render_template('denglu.html') else: name = request.form.get('name') passw = request.form.get('password') user = User.query.filter(User.username == name).first() if user: if user.password==passw: session['user']=name return redirect(url_for('manhua')) else: return "密码错误!" else: return "用户名不存在!"@app.context_processordef mycontext(): usern=session.get('user') if usern: return { 'name':usern} else: return {}@app.route('/logout')def logout(): session.clear() return redirect(url_for('manhua'))@app.route("/register",methods=['GET','POST'])def register(): if request.method == 'GET': return render_template("zhuce.html") else: name = request.form.get('name') passw = request.form.get('password') user = User.query.filter(User.username == name).first() if user: return "用户名已存在!" else: user = User(username=name, password=passw) db.session.add(user) db.session.commit() return render_template("denglu.html")if __name__ == '__main__': app.run(debug=True)