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):
__table__name = '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()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/regist/',methods=['GET','POST'])
def regist():
if request.method=='GET':
return render_template('regist.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 u'username existed'
else:
user=User (username=username,password=password,nickname=nickname)
db.session.add(user)
db.session.commit()
return redirect(url_for('login'))
@app.route('/login/',methods=['GET','POST'])
def login():
if request.method=='GET':
return render_template('login.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
return redirect(url_for('index'))
else:
return u'password error'
else:
return u'username is not existed'
@app.context_processor
def mycontext():
usern=session.get('user')
if usern:
return{'username':usern}
else:
return{}
@app.route('/logout/')
def logout():
session.clear()
return redirect(url_for('index'))
@app.route('/question/')
def question():
return render_template('question.html')
if __name__ == '__main__':
app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
<body bgcolor="#f5f5f5"></body>
<meta charset="UTF-8">
<title>{% block title%}DJI{% endblock %}</title>
<link href="{{ url_for('static',filename='css/base.css') }}" rel="stylesheet" type="text/css">
<script src="{{ url_for('static',filename='js/base.js') }}"></script>
{% block head %}{% endblock %}
</head>
<body id="myBody">
<nav>
<img id="myOnOff" onclick="mySwitch()" src="https://www.runoob.com/images/pic_bulbon.gif" height="20" width="20px">
<a href="{{ url_for('index') }}">首页</a>
<a href="http://www.dji.com/cn">消费级产品</a>
<a href="http://www.dji.com/cn">专业级产品</a>
<input type="text" name="search">
<button type="submit">搜索</button>
{% if username %}
<a href="#">{{ username }}</a>
<a href="{{ url_for('logout') }}">注销 </a>
{% else %}
<a href="{{ url_for('login') }}">登录</a>
<a href="{{ url_for('regist') }}">注册</a>
{% endif %}
<a href="{{ url_for('question') }}">发布问答</a>
</nav>
<div>
<p><span style="font-size: 50px;color:gold;font-family: '华文楷体', 'Deja Vu Sans Mono', 'Bitstream Vera Sans Mono', monospace";>DJI大疆创新
</span></p>
</div>
<div class="area">
{% block main %}{% endblock %}
<br>
</div>
<br>
<footer class="copyright">
<p>Copyright @ MarsGCC</p>
</footer>
<body background="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1510150688168&di=c33e4c9a7d66c191aa98f67dbffa08d5&imgtype=0&src=http%3A%2F%2Fimg.dixintong.com%2FInUpImg%2FSection_ProInfomation%2F2016-11-25%2F161125160647903500.jpg"style="background-repeat:no-repeat;background-size: 100% 100%; background-attachment: fixed;"></body>
</body>
</html>
</body>