博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
发布功能完成。
阅读量:4501 次
发布时间:2019-06-08

本文共 1273 字,大约阅读时间需要 4 分钟。

  • 编写要求登录的装饰器

from functools import wraps

def loginFirst(func): #参数是函数

@wraps(func)

      def wrapper(*args, ** kwargs): #定义个函数将其返回

          #要求登录

          return func(*args, ** kwargs)

      return wrapper #返回一个函数

  • 应用装饰器,要求在发布前进行登录,登录后可发布。
@app.route('/question/',methods=['GET','POST']) @loginFirst def question():

 

  • 建立发布内容的对象关系映射。
class Question(db.Model):
  • 完成发布函数。

保存到数据库。

重定向到首页。

@app.route('/fabu/',methods=['GET','POST'])@loginFirstdef fabu():    if request.method == 'GET':        return render_template('fabu.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('shouye')) # 跳转发布
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)    c520555reat_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'))

 

转载于:https://www.cnblogs.com/123hyf/p/7911656.html

你可能感兴趣的文章
CI Weekly #21 | iOS 持续集成快速入门指南
查看>>
利用DFS求联通块个数
查看>>
初识 python
查看>>
PCL Examples
查看>>
spring boot
查看>>
浏览器URL传参最大长度问题
查看>>
学习进度条
查看>>
Linux crontab 定时任务详解
查看>>
string成员函数
查看>>
onSaveInstanceState()方法问题
查看>>
[转]CocoaChina上一位工程师整理的开发经验(非常nice)
查看>>
大数据时代侦查机制有哪些改变
查看>>
雷林鹏分享:jQuery EasyUI 菜单与按钮 - 创建链接按钮
查看>>
Apache Traffic Server服务搭建
查看>>
poj1990两个树状数组
查看>>
学习python-day1
查看>>
Delphi的命令行编译命令
查看>>
BZOJ 1901 Zju2112 Dynamic Rankings 题解
查看>>
C++虚析构函数
查看>>
正则表达式之 数据验证 与 文本替换
查看>>