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


    主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')
  • 相关阅读:
    Eclipse扩展安装插件方式
    Tomcat通过JNDI方式链接MySql数据库
    ArrayList实现根据某属性大小相间排序
    JMeter 怎么保存登录状态
    JMeter怎么使用代理服务器
    JMeter模拟多个用户进行登录
    JMeter怎么在get URL请求、POST请求中添加动态参数用于服务器段安全验证
    (转)sizeof()和_countof()区别
    潜心修炼, 生活,会给你一个惊喜~
    最初的梦想
  • 原文地址:https://www.cnblogs.com/cch-1007/p/7989116.html
Copyright © 2020-2023  润新知