从首页问答标题到问答详情页
Posted 李海力
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从首页问答标题到问答详情页相关的知识,希望对你有一定的参考价值。
1.主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
2.首页标题的标签做带参数的链接。
{{ url_for(‘detail‘,question_id = foo.id) }}
{% block main %} <ul class="list1" > {% 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 %}
3.在详情页将数据的显示在恰当的位置。
{{ ques.title}}
{{ ques.id }}{{ ques.creat_time }}
{{ ques.author.username }}
{{ ques.detail }}
{% 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> <div> <input id="com" name="comment" type="text" placeholder="请输入内容" > <form action="{{ url_for(‘comment‘) }}" method="post"> <button>发送</button> </form> </div></ul> {% endblock %}4.建立评论的对象关系映射:
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‘))
以上是关于从首页问答标题到问答详情页的主要内容,如果未能解决你的问题,请参考以下文章