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

Flask路由与视图函数
本文介绍如何在Flask应用中定义带有参数的路由及视图函数,并展示了如何通过URL传递参数到视图函数中,同时演示了如何在前端模板中使用这些参数。
    1. 主PY文件写视图函数,带id参数。
    2. @app.route('/detail/<question_id>')
      def detail(question_id):
          quest = 
          return render_template('detail.html', ques = quest)
       #详情页
       @app.route('/detail/<questions_id>')
       def detail(questions_id):
           questions=Question.query.filter(Question.id==questions_id).first()
           return render_template('detail.html',questions=questions)

       

    3. 首页标题的标签做带参数的链接。
      1. {{ url_for('detail',question_id = foo.id) }}
复制代码
  {% block main %}
      <p>{{ username }} contextx</p>
      <ul class="new-list">
          {% for foo in questions %}
              <li class="list-group"
                  style="padding-left: 0px; padding-right: 10px; box-shadow: rgba(0, 0, 0, 0.498039) 0px 0px 0px 0px;">
                  <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>
                  <a href="#">{{ foo.author.username }}</a><br>
                  <a href="{{ url_for('detail',questions_id=foo.id) }}">{{ foo.title }}</a>{#链接到相关详情页#}
                 <span class="badge" >{{ foo.create_time }}</span>
                 <p>{{ foo.detail }}</p>
             </li>
         {% endfor %}
     </ul>
 {% endblock %}
复制代码

 

    1. 在详情页将数据的显示在恰当的位置。
    2. {{ ques.title}}
      {{ ques.id  }}{{  ques.creat_time }}
      {{ ques.author.username }} 
      {{ ques.detail }}
复制代码
  {% extends'base.html' %}
  
  {% block title %}问答详情{% endblock %}
  {% block head %}
      <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/detail.css') }}">
  {% endblock %}
  
  {% block main %}
      <div class="page-header">
     <h3>题目:{{ questions.title }}<br><small>作者:{{ questions.author.username }} <span class="badge">发布时间:{{ questions.create_time }}</span></small></h3>
     </div>
     <p class="lead">详情:{{ questions.detail }}</p><br>
 
     {#发布评论#}
     <form action="{{ url_for('questions') }}" method="post">
         <div class="form-group">
             <textarea name="new_comment" class="form-control" rows="3" id="new-comment"
                       placeholder="请写下你的评论"></textarea>
             <input type="hidden" name="questions_id" value="{{ questions.id }}">
         </div>
         <button type="submit" class="btn btn-default" onclick="fnDetail">发送</button>
 
     {# 评论列表#}
     </form>
     <h4>评论({{ questions.comments|length }})</h4>
     <ul class="list-group" style="margin: 10px"></ul>
 {% endblock %}
复制代码

转载于:https://www.cnblogs.com/y550293904/p/7989822.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值