从首页问答标题到问答详情页

主PY文件写视图函数,带id参数。 

         @app.route('/detail/<question_id>')
            def detail(question_id):
             quest = 
          return render_template('detail.html', ques = quest)

 

@app.route('/detail<question_id>')
def detail(question_id):
    quest = Question.query.filter(Question.id == question_id).first()

    return render_template('detail.html',quest = quest)

 

 

首页标题的标签做带参数的链接。
          {{ url_for('detail',question_id = foo.id) }}

<!DOCTYPE html>
<html lang="en">
<head>
    {% extends 'test1.html' %}
    <meta charset="UTF-8">
    <title>{% block title %}
        首页
    {% endblock %}</title>

    {% block head %}
        <link rel="stylesheet" type="text/css" href="../static/css/admin.css">
        <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    {% endblock %}


</head>

<body>


{% block body %}

    <p>{{ user }}context</p>

    <div class="col-md-4 column">
        <ul>
            {% for foo in questions %}
                <li>
                    <a href="{{ url_for('detail',question_id = foo.id) }}">{{ foo.title}}</a><br>
                    <a href="#">{{ foo.title }}</a><br>
                    <p>{{ foo.detail }}</p>
                    <a href="{{ url_for('usercenter',user_id = foo.author_id) }}">{{ foo.author.username}}评论:({{ foo.comments|length }})</a>
                    <span class="t">{{ foo.creat_time }}</span>

                </li>
            {% endfor %}
        </ul>
    </div>


{% endblock %}


</body>
</html>

 

在详情页将数据的显示在恰当的位置。 

         {{ ques.title}}
         {{ ques.id  }}{{  ques.creat_time }}

         {{ ques.author.username }} 
         {{ ques.detail }}

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block title %}
        提问详情
     {% endblock %}</title>

{% block head %}


{% endblock %}

</head>
<body>
{% block body %}

<h1>提问详情</h1>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <h3 class="text-center">
                {{ quest.title }}<br>
                <small>{{ quest.author.username }}
                <span class="">
                    {{ quest.creat_time }}
                </span>
                </small>
            </h3>
            <p class="text-center">
                 {{ quest.detail }}
            </p>
            <hr>
            <form role="form">
                <div class="form-group">
                     <label for="exampleInputEmail1">Write down your answer~~</label><input type="email" class="form-control" id="exampleInputEmail1" />
                </div>
                 <button type="submit" class="btn btn-default">Submit</button>
            </form>
        </div>
    </div>
</div>
{% endblock %}


</body>
</html>

 

建立评论的对象关系映射:

             class Comment(db.Model):
               __tablename__='comment'

 尝试实现发布评论。

 

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'))
    creat_time = db.Column(db.DateTime,default=datetime.now)
    detail = db.Column(db.Text,nullable=False)
    question = db.relationship('Question',backref = db.backref('comments'))
    author = db.relationship('User',backref = db.backref('comments'))

 

posted on 2017-12-06 00:35  043李庆  阅读(118)  评论(0编辑  收藏  举报

导航