划过的梦神 2018-09-19 02:33 采纳率: 33.3%
浏览 1677
已采纳

django 怎么数据加载部分数据到首页?

我的网站首页导航到其他网页,其他数据都是从数据库读取的数据。我想要在首页显示几条最近的数据,例如只显示新闻页面的前5条,点击读取更多跳转新闻页面。我的首页使用table,一部分填充了news_widget.html页面。
我的 new_widget.html代码如下:

 <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><img src="/static/img/main/news.gif" width="131" height="41" /></td>
  </tr>
  <tr>
    <td background="/static/img/dot_white.gif"><img src="/static/img/blank.gif" width="1" height="1" /></td>
  </tr>
  {%for item in news%}
  <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="69"><img src="/static/img/main/center_{{forloop.counter}}.gif" width="69" height="72" /></td>
        <td style="line-height:18px;"><strong><a href="/news/{{item.id}}">{{item.title}}</a></strong><br />
            {{item.content|slice:":50"}}...
        </td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td background="/static/img/dot_white.gif"><img src="/static/img/blank.gif" width="1" height="1" /></td>
  </tr>
  {%endfor%} 
  <tr>
    <td height="30">&nbsp;</td>
  </tr>
  <tr>
    <td><a href="/about"><img src="/static/img/main/center_overview.gif" width="273" height="55" border="0" /></a></td>
  </tr>
  <tr>
    <td style="padding:0 23px 0 13px;">
    {{about.content|slice:":200"}}
    <br />
    <a href="/about/{{about.catid}}" class="arial_12_orange_a">了解更多</a> 
    </td>
  </tr>
</table>

这样写会把我所有数据库里的新闻加载到首页。有没有什么办法只加载前几个?

  • 写回答

1条回答 默认 最新

  • qq_23885999 2018-09-24 14:22
    关注

    你可以采用分页的办法呀,规定每页显示几条

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog