from functools import wrapshtml
def loginFirst(func): #參數是函數數據庫
@wraps(func)session
def wrapper(*args, ** kwargs): #定義個函數將其返回app
#要求登陸函數
return func(*args, ** kwargs)url
return wrapper #返回一個函數spa
def loginFirst(func): @wraps(func) def wrapper(*args,**kwargs): if session.get('user'): return func(*args,**kwargs) else: return redirect(url_for('login'))
return wrapper
@app.route('/question/',methods=['GET','POST'])
@loginFirst
def question():
@app.route('/question/',methods=['GET','POST']) @loginFirst def questions():
class Question(db.Model):
class Question(db.Model): __tablename__='question' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title =db.Column(db.String(100), nullable=False) detail = db.Column(db.Text, nullable=False) creat_time=db.Column(db.DateTime,default=datetime.now) author_id = db.Column(db.Integer,db.ForeignKey('User.id')) author=db.relationship('User',backref=db.backref('question')) db.create_all()
保存到數據庫。code
重定向到首頁。orm
@app.route('/question/',methods=['GET','POST']) @loginFirst def questions(): if request.method=='GET': return render_template('questions.html') else: title=request.form.get('title') detail=request.form.get('detail') author_id=User.query.filter(User.username==session.get('user')).first().id question=Question(title=title,detail=detail,author_id=author_id) db.session.add(question) db.session.commit() return redirect(url_for('base'))