博客
关于我
强烈建议你试试无所不能的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

你可能感兴趣的文章
Rsync详解
查看>>
【每日一读】Java编程中“为了性能”尽量要做到的一些地方
查看>>
什么是内网、什么是公网、什么是NAT
查看>>
【堆/排序】堆排序的两种建堆方法
查看>>
类的内置方法
查看>>
项目中使用的第三方开源库
查看>>
NOIP2009 潜伏者
查看>>
本地预览的vue项目,在githubpage静态展示
查看>>
SC命令---安装、开启、配置、关闭 cmd命令行和bat批处理操作windows服务
查看>>
Register Form Code
查看>>
iphone 如何清空UIWebView的缓存
查看>>
Java——变量
查看>>
定时关闭AWS上的EC2机器实例
查看>>
grep、awk、sed命令详解1
查看>>
Jenkins邮件配置
查看>>
MYSQL数据库的设计与调优
查看>>
在Apache下开启SSI配置
查看>>
多线程上下文切换
查看>>
基于django后端的html、js简单实现含中文csv文件下载
查看>>
MySQL的InnoDB的幻读问题
查看>>