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


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

    @app.route('/detail/<question_id>')
    def detail(question_id):
        quest = 
        return render_template('detail.html', ques = quest)
    1. 首页标题的标签做带参数的链接。
            {{ url_for('detail',question_id = foo.id) }}

    首页代码

    {% extends 'ba.html' %}
    {% block title %}
        首页
    {% endblock %}
    {% block main %}
        <link rel="stylesheet" type="text/css" href="../static/css/wenda.css">
        <p style="margin-left: 15%">欢迎您:{{ username }}</p>
        <div class="box">
            <ul class="list-group">
                {% for foo in questions %}
                    <li class="list-group-item">
                        <img style=" 30px" src="{{ url_for('static',filename='css/touxiang.jpg') }}" alt="64">
                        <a href="#">{{ username }}</a><br>
                        <a href="{{ url_for('detail',question_id=foo.id) }}">问题:{{ foo.title }}</a><br>
                        <p style="align-content: center">{{ foo.detail }}</p>
                        <span class="badge" style="margin-left: 60%">{{ foo.create_time }}</span>
                        <p style="margin-left: 25%"></p><br>
                    </li>
                {% endfor %}
            </ul>
        </div>
    {% endblock %}

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

    {{ ques.title}}
    {{ ques.id  }}{{  ques.creat_time }}
    {{ ques.author.username }} 
    {{ ques.detail }}
    {% extends 'ba.html' %}
    {% block title %}
        问答详情
    {% endblock %}
    {% block main %}
        <div class="box">
            <h3 href="#" >{{ ques.title }}</h3><small> {{ ques.author.username }}<span class="badge" style="margin-left: 75%">{{ ques.create_time }}</span></small>
            <hr>
            <p>{{ ques.detail }}</p>
            <hr>
            <form>
                <div><textarea class="form-control" id="comment" rows="3" style="margin-left: 1%" name="comment" placeholder="write your comment"></textarea><br></div>
                <button type="submit" >发送</button>
            </form>
          <h4>评论:</h4>
             <ul class="list-group">
                    <li class="list-group-item">
                        <img style=" 30px" src="{{ url_for('static',filename='css/touxiang.jpg') }}" alt="64">
                        <a href="#"></a><br>
                        <p style="align-content: center"></p>
                        <span class="badge" style="margin-left: 60%"></span>
                        <p style="margin-left: 25%"></p><br>
                    </li>
            </ul>
    
        </div>
    {% endblock %}
    1. 建立评论的对象关系映射:

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

    2. 尝试实现发布评论。

    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'))
         create_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')
  • 相关阅读:
    Class的用途
    Flash网络编程安全沙箱问题隆重解决 (转)
    带参数的EventDispatcher
    Object的效率
    Oracle数据库语言修改成UTF8
    Python之字符串详解1
    初级/中级/高级运维,你是哪一级?
    这可能是php世界中最好的日志库——monolog
    vc程序大小优化最佳方案(转)http://blog.sina.com.cn/s/blog_4c50333c0100gjs3.html
    C# 调用lua 报错未能加载文件或程序集“lua51.dll”或它的某一个依赖项。找不到指定的模块。 解决方法
  • 原文地址:https://www.cnblogs.com/cch-1007/p/7989116.html
Copyright © 2020-2023  润新知