从首页问答标题到问答详情页
- 主PY文件写视图函数,带id参数。
@app.route('/detail/<question_id>')
def detail(question_id):
quest =
return render_template('detail.html', ques = quest)@app.route('/detail/<question_id>') @log_re def detail(question_id): quest=Question.query.filter(Question.id==question_id).first() return render_template("detail.html",ques=quest) return question_id
- 首页标题的标签做带参数的链接。
{{ url_for('detail',question_id = foo.id) }}
{% extends 'base.html' %} {% block title %} 首页 {% endblock %} {% block head %} <link type="text/css" rel="stylesheet" href="{{ url_for('static',filename='css/index.css') }}"> {% endblock %} {% block main %} <ul class="list1" > <img src="../static/images/myself.jpg" width="60" height="60"> {% for foo in questions %} <li class="list-group-item"> <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span> {% if username %} <li class="zero"><a href="{{ url_for('detail',question_id=foo.id) }}">{{ username }}</a></li> {% endif %} <a class="#"></a> <br> <span class="badge">{{ foo.creat_time }}</span> <li class="one" a href="#">{{ foo.title }}</li> <li class="two"><p style="text-indent: 18px">{{ foo.detail }}</p></li> </li> {% endfor %} </ul> {% endblock %}
- 在详情页将数据的显示在恰当的位置。
{{ ques.title}}
{{ ques.id }}{{ ques.creat_time }}{{ ques.author.username }}
{{ ques.detail }}{% extends 'base.html' %} {% block title %} 问答详情页 {% endblock %} {% block head %} <link type="text/css" rel="stylesheet" href="../static/css/detail.css"> {% endblock %} {% block main %} <ul class="list"> <p class="title">{{ ques.title }}</p> <a href="usern">{{ ques.author.username }}</a> <p class="answer" style="text-indent: 18px">{{ ques.detail }}</p> <span class="badge">{{ ques.creat_time }}</span> <a href="a">回复♥</a> <a href="b">送礼</a> <div> <input id="com" name="comment" type="text" placeholder="我也说一句..." > <form action="{{ url_for('comment') }}" method="post"> <button>发送</button> </form> </div></ul> {% endblock %}
-
建立评论的对象关系映射:
class Comment(db.Model):
__tablename__='comment'@app.route('/comment/',methods=['POST']) @log_re def comment(): user = User.query.filter(User.username == session.get('user')).first() author_id = user.id question = Question.query.filter(Question.author_id == author_id ).first() question_id = question.id comment = Comment(author_id=author_id,question_id=question_id,detail=detail) db.session.add(comment) # 保存到数据库 db.session.commit() # 提交 return redirect(url_for('index'))
-
尝试实现发布评论。