博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从首页问答标题到问答详情页
阅读量:4568 次
发布时间:2019-06-08

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

 

  1. 主PY文件写视图函数,带id参数。 
    @app.route('/detail/<question_id>')
    def detail(question_id):
        quest = 
        return render_template('detail.html', ques = quest) 
  2. 首页标题的标签做带参数的链接。
          {
    { url_for('detail',question_id = foo.id) }}
  3. 在详情页将数据的显示在恰当的位置。 
    {
    { ques.title}}
    {
    { ques.id  }}{
    {  ques.creat_time }}
    {
    { ques.author.username }} 
    {
    { ques.detail }}
  4. 建立评论的对象关系映射:

    class Comment(db.Model):

        __tablename__='comment'

  5.  尝试实现发布评论。

 

class Comment(db.Model):    _tablename_='comment'    id=db.Column(db.Integer,primary_key=True,autoincrement=True)    author_id=db.Column(db.Integer,db.ForeignKey('User.id'))    question_id=db.Column(db.Integer,db.ForeignKey('question.id'))    detail=db.Column(db.Text,nullable=False)    creatTime=db.Column(db.DateTime,default=datetime.now)    question=db.relationship('Question',backref=db.backref('comments',order_by=creatTime.desc))    author=db.relationship('User',backref=db.backref('comments'))#db.create_all()@app.route('/')def home():    context={        'question': Question.query.all()    }    return render_template('shouye.html',**context)@app.route('/question_detail/
')def question_detail(question_id): question = Question.query.filter(Question.id == question_id).first() return render_template('question_detail.html',question=question)@app.route('/comment/',methods=['GET','POST'])# @loginFirstdef comment(): if request.method == 'GET': return render_template('question_detail.html') else: detail = request.form.get('detail') author_id =User.query.filter(User.username == session.get('user')).first().id #question_id=Question.query.filter(Question.authorID ==author_id).first().id #question=Question.query.filter(Question.authorID == session.get('question')).first() #user = User.query.filter(User.username == session.get('user')).first() comments = Comment(detail=detail,author_id=author_id,question_id=question_id) #comments.question=question #comments.author = user db.session.add(comments) db.session.commit() return redirect(url_for('home'))if __name__ == '__main__': app.run(debug=True)
{% extends'base.html' %}{% block title %}    Home{% endblock %}{% block head %}    
{% endblock %}{% block main %}

{
{ question.title }}

{
{ question.author.username }}
{
{ question.creatTime }}

{
{ question.detail }}

评论:({

{ question.comments|length }})

{% endblock %}

转载于:https://www.cnblogs.com/lkm123/p/7995310.html

你可能感兴趣的文章
浏览器兼容问题笔记
查看>>
OUTLOOK+VBA 备份邮件到GMAIL
查看>>
谁说delphi没有IOCP库,delphi新的IOCP类库,开源中: DIOCP组件JSON流模块说明
查看>>
[四种方法]检测数据类型
查看>>
chrome使用技巧 值得珍藏
查看>>
【IT笔试面试题整理】数组中出现次数超过一半的数字
查看>>
cocos下的UI编辑器--BoomEditor使用教程(1)--快速上手
查看>>
关于树的字段设计
查看>>
display, position和float的关系
查看>>
查看linux错误日志
查看>>
go语言之进阶篇 select实现的超时机制
查看>>
C++ Primer 有感(异常处理)(三)
查看>>
支付宝支付
查看>>
lambda表达式多条件查询
查看>>
[百度之星2014资格赛] Disk Schedule 报告
查看>>
控制台应用程序《石头剪刀布》——新手,
查看>>
移动前端自适应解决方案和比较
查看>>
NSString用法总结
查看>>
Use formatter to format your JAVA code
查看>>
Go prepare statment超过mysql最大数
查看>>