---恢复内容开始---
- 主PY文件写视图函数,带id参数。
@app.route('/detail/<question_id>')
def detail(question_id):
quest =
return render_template('detail.html', ques = quest)@app.route('/question/',methods=['GET','POST']) @loginFirst def question(): if request.method=='GET': return render_template('question.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('myweb')) @app.route('/detail/<question_id>') def detail(question_id): quest=Question.query.filter(Question.id == question_id).first() return render_template('detail.html',ques=quest)
- 首页标题的标签做带参数的链接。
{{ url_for('detail',question_id = foo.id) }}
{% block main%} <img src="{{url_for('static',filename='images/qalogo.png')}}" alt="qa"> <ul class="list-group" style=""> <!--<p>{{user}} context</p>--> {% for foo in question %} <li class="list-group-item"> <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span> <a href="{{url_for('detail',question_id=foo.id)}}">{{foo.title}}</a> <p style="">{{foo.detail}}</p> <span class="glyphicon glyphicon-user" aria-hidden="true"></span> <a href="{{url_for('usercenter',user_id=foo.author_id)}}">{{foo.author.username}}评论:({{foo.comments|length}})</a> <span class="badge">{{foo.creat_time}}</span> <p style="color:black">内容</p> </li> {% endfor %} </ul> {% endblock %}
- 在详情页将数据的显示在恰当的位置。
{{ ques.title}}
{{ ques.id }}{{ ques.creat_time }}{{ ques.author.username }}
{{ ques.detail }} -
建立评论的对象关系映射:
class Comment(db.Model):
__tablename__='comment' -
尝试实现发布评论。
<div class="page-header" xmlns="http://www.w3.org/1999/html"> <h3>{{ques.id}}{{ques.title}}<br> <small>{{ques.author.username}}<span class="badge"{{ques.creat_time}}></span></small></h3> </div> <p class="lead">{{ques.detail}}</p> <hr> <form action="{{url_for('comment')}}" method="post" style=""> <div class="form-group"> <textarea name="new_comment" class="form-control" rows="3" id="new-comment" placeholder="write your comment"></textarea> <input type="hidden" name="question_id" value="{{ques.id}}"> </div> <button type="submit" class="btn btn-default">发送</button> </form> <!--<h4>评论:({{ques.comments|length}})</h4>--> <!--<ul class="list-group" style="">--> <!--{% for foo in ques.comments%}--> <!--<li class="list-group-item">--> <!--<span class="=glyphicon glyphicon-heart-empty " aria-hidden="true"></span>--> <!--<a href="#">{{foo.author.username}}</a>--> <!--<span class="badge">{{foo.creat_time}}</span>--> <!--<p style="">{{foo.detail}}</p>--> <!--</li>--> {%endfor%} </ul>
---恢复内容结束---