项目结构
│ bbs_demo.py # 视图文件
│ config.py # 存放配置文件
│ exts.py # 解决循环引用, 存放db
│ manage.py # 用flask-script管理
│ models.py # ORM
│
├─.idea
│ │ bbs_demo.iml
│ │ misc.xml
│ │ modules.xml
│ │ workspace.xml
│ │
│ └─inspectionProfiles
├─static # 存放静态文件,如图片,css
└─templates # 存放模版
flask_project.py
存放视图函数
from flask import Flask, render_template, request, redirect, url_for, session
from models import User, Issue, Comment
from exts import db, valiEmail
from decorators import login_required
import config
app = Flask(__name__)
app.config.from_object(config)
db.init_app(app)
@app.route('/', methods=['GET', 'POST'])
def index():
context = {
'issues' : Issue.query.order_by(db.desc('create_time')).all()
}
return render_template('index.html', **context)
@app.route('/d/<issue_id>')
def d(issue_id):
res = Issue.query.filter(Issue.id == issue_id).first()
return render_template('d.html', issue_item = res)
@app.route('/add_comment/', methods=["POST"])
@login_required
def add_comment():
comment = Comment(comment=request.form.get('comment'))
issue_id = request.form.get('issue_id')
user_id = session['user_id']
user = User.query.filter(User.id==user_id).first()
issue = Issue.query.filter(Issue.id == issue_id).first()
comment.author = user
comment.issue = issue
db.session.add(comment)
db.session.commit()
return redirect(url_for('d', issue_id=issue_id))
@app.route('/issue/', methods=['GET', 'POST'])
@login_required
def issue():
if request.method == 'GET':
return render_template('issue.html')
else:
title = request.form.get('title')
content = request.form.get('content')
issue = Issue(title=title, content=content)
user_id = session.get('user_id')
user = User.query.filter(User.id == user_id).first()
issue.author = user
db.session.add(issue)
db.session.commit()
return redirect(url_for('index'))
@app.route('/login/', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
else:
email = request.form.get('email')
password = request.form.get('password')
user = User.query.filter(User.email==email, User.password==password).first()
if user:
session['user_id'] = user.id
return redirect(url_for('index'))
else:
return redirect(url_for('login'))
@app.route('/regist/', methods=['GET', 'POST'])
def regist():
if request.method == 'GET':
return render_template('regist.html')
else:
email = request.form.get('email')
username = request.form.get('username')
password1 = request.form.get('password1')
password2 = request.form.get('password2')
user = User.query.filter(User.email == email).first()
if user:
return u"该邮箱已存在!"
elif not valiEmail(email):
return u"请检查邮箱格式I"
else:
if password1 == password2:
user = User(email=email, username=username, password=password1)
db.session.add(user)
db.session.commit()
return redirect(url_for('login'))
@app.route('/logout/')
def logout():
# session.pop('user_id')
# del session['user_id']
session.clear()
return redirect(url_for('login'))
@app.context_processor
def valiLogin():
user_id = session.get('user_id')
if user_id:
user = User.query.filter(User.id == user_id).first()
if user:
return {'user':user}
return {}
if __name__ == '__main__':
app.run()
config.py
import os
#DEBUG = True
# 设置session需要用到的字段
SECRET_KEY = os.urandom(24)
# dialect+driver://username:password@host:port/database
DIALECT = 'mysql'
DRIVER = 'mysqldb'
USERNAME = 'root'
PASSWORD = 'root'
HOST = '127.0.0.1'
PORT = '3306'
DATABASE = 'bbs_demo'
SQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(DIALECT,DRIVER,USERNAME,PASSWORD,
HOST,PORT,DATABASE)
在主视图文件中使用 app.config.from_object(config)
引入配置文件
exts.py
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
需要注意的是db = SQLAlchemy()
使用都需要绑定一个app.不然会导致迁移失败.这里在manage.py
进行绑定.不然会导致循环引用
manage.py
类似django
一样使用manage用命令行方式方便地进行操作,这里主要是数据库的迁移工作.
from flask_script import Manager
from flask_migrate import MigrateCommand, Migrate
from bbs_demo import app
from exts import db
# 创建的模型都需要导入到这个文件中
# MigrateCommand才会讲模型映射到数据库中去
from models import User
# Manage()需要给定一个app
manager =Manager(app)
# SQLalchemy运行之前需要绑定到当前app
db.init_app(app)
# Migrate绑定app和db
migrate = Migrate(app, db)
# 添加迁移脚本的命令到manager
manager.add_command('db', MigrateCommand)
if __name__ =="__main__":
manager.run()
主要操作命令有:
* python manage.py db init
* python manage.py db migrate
* python manage.py db upgrade
模型导入
在这个脚本写完之后,之后需要将模型映射到数据库中的准备工作:
* 创建模型到models.py中
* manage中导入创建好的模型
* 使用命令行进行映射
models.py
from exts import db
# 定义一些模型
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
email = db.Column(db.String(30), nullable=False)
username = db.Column(db.String(30), nullable=False)
password = db.Column(db.String(50), nullable=False)
在manage.py
中导入模型
from models.py import User
decorators.py
存放一些装饰器
from functools import wraps
from flask import session, redirect, url_for
def login_required(func):
@wraps(func)
def wrapper(*args, **kwargs):
if session.get('user_id'):
return func(*args, **kwargs)
else:
return redirect(url_for('login'))
return wrapper
至此就是项目的全部内容