from flask import Flask,render_template,request,redirect,url_for,session
from flask_sqlalchemy import SQLAlchemy
import config
app = 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(20))
db.create_all()
# user=User(username='mis1234',password='111111')
# db.session.add(user)
# db.session.commit()
@app.route('/')
def base():
return render_template("base.html")
@app.route('/login/', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template("denglu.html")
else:
username = request.form.get('username')
password = request.form.get('password')
user = User.query.filter(User.username == username).first()
if user:
if user.password == password:
session['user']=username
session.permanent=True
return redirect(url_for('base'))
else:
return '密码错误'
else:
return '用户名不存在'
@app.context_processor
def mycontext():
usern = session.get('username')
if usern:
return {'username':usern}
else:
return {}
@app.route('/regist/', methods=['GET', 'POST'])
def zhuce():
if request.method == 'GET':
return render_template("zhuce.html")
else:
username = request.form.get('username')
password = request.form.get('password')
nickname = request.form.get('nickname')
user = User.query.filter(User.username == username).first()
if user:
return '用户名已存在'
else:
user = User(username=username, password=password, nickname=nickname)
db.session.add(user) # 数据库,添加操作
db.session.commit()
return redirect(url_for('login'))
@app.route('/logout/')
def logout():
session.clear()
return redirect(url_for('base'))
@app.route('/fabu/')
def fabu():
return render_template('question.html')
if __name__=='__main__':
app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}base</title>
<link rel="stylesheet" href="../static/css/base.css" type="text/css">
<script src="../static/js/base.js"></script>
{% block head %}{% endblock %}
</head>
<body id="myBody">
{% block main %}{% endblock %}
<nav>
<div class="qwe">
<div class="qwe" style="padding-top:10px;">
<img id="myOnOff" onclick="myswitch()" src="http://www.runoob.com/images/pic_bulbon.gif" width="40px">
{% if username %}
<li><a href="#">{{ username }}</a></li>
<li style="float:left"><a href="{{ url_for('base') }}">注销</a></li>
{% else %}
<li style="float:left"><a href="{{ url_for('login') }}">登陆</a></li>
<li style="float:left"><a href="{{ url_for('zhuce') }}">注册</a></li>
{% endif %}
<li style="float:left"><a href="{{ url_for('base')}}">首页</a></li>
<li style="float:left"><a href="{{ url_for('fabu')}}">发布</a></li>
</div>
</div>
<br>
<form method="get" action="#">
<div class="qwe1">
<div class="qwe2" style="padding-left: 50px;">
<input type="text" name="title" required lay-verify="required"
placeholder="search"
autocomplete="off" class="layui-input">
</div>
<div class="qwe3" style="padding-left: 50px;">
<button type="submit" class="layui-btn layui-btn-primary">搜索</button>
</div>
</div>
</form>
</nav>
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}{% endblock %}base</title>
<link rel="stylesheet" href="../static/css/base.css" type="text/css">
<script src="../static/js/base.js"></script>
{% block head %}{% endblock %}
</head>
<body id="myBody">
{% block main %}{% endblock %}
<nav>
<div class="qwe">
<div class="qwe" style="padding-top:10px;">
<img id="myOnOff" onclick="myswitch()" src="http://www.runoob.com/images/pic_bulbon.gif" width="40px">
{% if username %}
<li><a href="#">{{ username }}</a></li>
<li style="float:left"><a href="{{ url_for('base') }}">注销</a></li>
{% else %}
<li style="float:left"><a href="{{ url_for('login') }}">登陆</a></li>
<li style="float:left"><a href="{{ url_for('zhuce') }}">注册</a></li>
{% endif %}
<li style="float:left"><a href="{{ url_for('base')}}">首页</a></li>
<li style="float:left"><a href="{{ url_for('fabu')}}">发布</a></li>
</div>
</div>
<br>
<form method="get" action="#">
<div class="qwe1">
<div class="qwe2" style="padding-left: 50px;">
<input type="text" name="title" required lay-verify="required"
placeholder="search"
autocomplete="off" class="layui-input">
</div>
<div class="qwe3" style="padding-left: 50px;">
<button type="submit" class="layui-btn layui-btn-primary">搜索</button>
</div>
</div>
</form>
</nav>
</body>
</html>