在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python/ flask模板如何調(diào)用出數(shù)據(jù)

flask模板如何調(diào)用出數(shù)據(jù)

文章表
news表
id url title content classid
1 http://www.xxx.com/1/a.html 標(biāo)題1 內(nèi)容1 1
2 http://www.xxx.com/2/b.html 標(biāo)題2 內(nèi)容2 2
3 http://www.xxx.com/3/c.html 標(biāo)題3 內(nèi)容3 3
4 http://www.xxx.com/1/e.html 標(biāo)題4 內(nèi)容4 1
5 http://www.xxx.com/2/w.html 標(biāo)題5 內(nèi)容5 2
......還有很多數(shù)據(jù)......

欄目表
class表
id name
1 欄目1
2 欄目2
3 欄目3
......還有很多數(shù)據(jù)......

views.py

news = News.query.join(
    Class
).order_by(
    News.id.desc()
)..paginate(1,5))
或者
news = News.query.join(
    Class
).order_by(
    News.id.desc()
).limit(5) 用這個(gè) for循環(huán)就不需要.items

模板調(diào)用數(shù)據(jù)

{% for v in news.items %}
{% if loop.index is odd %}
<div class="pnvt_news_model ">
<dl class="title len">
<span class="sub "><i></i><a href="/{{ v.class.name }}/" target="_blank">{{ v.class.name }}</a></span>
</dl>
<dl class="html nh215">
<dt class="html">
<p><a href="{{ v.url }}" title="{{ v.title }}" target="_blank">{{ v.title }}</a></p>
</dt>
</dl>
</div>
{% else %}
<div class="pnvt_news_model pr0">
<dl class="title">
<span class="sub "><i></i><a href="/{{ v.class.name }}/" target="_blank">{{ v.class.name }}</a></span>
</dl>
<dl class="html nh215">
<dt class="html">
<p><a href="{{ v.url }}" title="{{ v.title }}" target="_blank">{{ v.title }}</a></p>
</dt>
</dl>
</div>
{% endif %}
{% endfor %}

上面代碼只能循環(huán)出一個(gè)欄目,我想循環(huán)出所有欄目,并取出該欄目下面5篇文章內(nèi)容。
下面是想得到的效果源碼:

<div class="pnvt_news_model ">
<dl class="title len">
<span class="sub "><i></i><a href="/欄目1/" target="_blank">欄目1</a></span>
</dl>
<dl class="html nh215">
<dt class="html">
<p><a href="" title="標(biāo)題1" target="_blank">標(biāo)題1</a></p>
<p><a href="" title="標(biāo)題1" target="_blank">標(biāo)題1</a></p>
<p><a href="" title="標(biāo)題1" target="_blank">標(biāo)題1</a></p>
<p><a href="" title="標(biāo)題1" target="_blank">標(biāo)題1</a></p>
<p><a href="" title="標(biāo)題1" target="_blank">標(biāo)題1</a></p>
#這里顯示欄目1中的5篇文章取出來
</dt>
</dl>
</div>
<div class="pnvt_news_model pr0">
<dl class="title">
<span class="sub"><i></i><a href="/欄目2/" target="_blank">欄目2</a></span>
</dl>
<dl class="html nh215">
<dt class="html">
<p><a href="" title="標(biāo)題2" target="_blank">標(biāo)題2</a></p>
<p><a href="" title="標(biāo)題2" target="_blank">標(biāo)題2</a></p>
<p><a href="" title="標(biāo)題2" target="_blank">標(biāo)題2</a></p>
<p><a href="" title="標(biāo)題2" target="_blank">標(biāo)題2</a></p>
<p><a href="" title="標(biāo)題2" target="_blank">標(biāo)題2</a></p>
#這里顯示欄目2中的5篇文章取出來
</dt>
</dl>
</div>

求各位大神指點(diǎn),先謝謝大家的回答

回答
編輯回答
笨尐豬

1.先取出所有欄目表,也就是取出class表所有分類 class.query.all() #這個(gè)取出來也是list
2.循環(huán)這個(gè)分類 取出每個(gè)分類的5篇文章,然后放到一個(gè)list里面

list1 = ["文章1","文章2","文章3","文章4","文章5"]["文章1","文章2","文章3","文章4","文章5"]["文章1","文章2","文章3","文章4","文章5"]  #根據(jù)分類循環(huán)出來

3.把第一步class取出來的list 和第二部循環(huán)出來的弄成個(gè)字典

內(nèi)容大概顯示{"欄目1":["文章1","文章2","文章3","文章4","文章5"],"欄目2":["文章1","文章2","文章3","文章4","文章5"]}
test = {"欄目1":["文章1","文章2","文章3","文章4","文章5"],"欄目2":["文章1","文章2","文章3","文章4","文章5"]}

4.頁面顯示內(nèi)容

{% for v,k in test.items() %}
<div class="{% if loop.index is odd %}pnvt_news_model{% else %}pnvt_news_model pr0{% endif %}">
<dl class="title len">
<span class="sub "><i></i><a href="/{{ v.name }}" target="_blank">{{ v.txt }}</a></span>
</dl>
<dl class="html nh215">
<dt class="html">
{% for i in k %}
{% if v.id == i.column_id %}
<p><a href="{{ i.url }}" title="{{ i.title }}" target="_blank">{{ i.title }}</a></p>
{% endif %}
{% endfor %}
</dt>
</dl>
</div>
{% endfor %}

暫時(shí)只想出這個(gè)辦法解決,有其他解決辦法歡迎指導(dǎo)我。

2018年1月14日 14:19
編輯回答
憶當(dāng)年

不是很明白你的問題,但是建議看看 jinja2 語法中關(guān)于 for 循環(huán)的 loop變量。

循環(huán)控制語句及宏(宏類似于Python代碼中的函數(shù)):

{% macro render_comment(comment) %}
   <li>{{ comment }}</li>
{% endmacro %}
<ul>
   {% for comment in comments %}
     {{ render_comment(comment) }}
   {% endfor %}
</ul>

在一個(gè) for 循環(huán)塊中你可以訪問這些特殊的變量:

變量 描述
loop.index 當(dāng)前循環(huán)迭代的次數(shù)(從 1 開始)
loop.index0 當(dāng)前循環(huán)迭代的次數(shù)(從 0 開始)
loop.revindex 到循環(huán)結(jié)束需要迭代的次數(shù)(從 1 開始)
loop.revindex0 到循環(huán)結(jié)束需要迭代的次數(shù)(從 0 開始)
loop.first 如果是第一次迭代,為 True 。
loop.last 如果是最后一次迭代,為 True 。
loop.length 序列中的項(xiàng)目數(shù)。
loop.cycle 在一串序列間期取值的輔助函數(shù)。見下面的解釋。

在 for 循環(huán)中,可以使用特殊的 loop.cycle 輔助函數(shù),伴隨循環(huán)在一個(gè)字符串/變 量列表中周期取值:

這里估計(jì)是你想要的。

{% for row in rows %}
   <li class="{{ loop.cycle('odd', 'even') }}">{{ row }}</li>
{% endfor %}

詳情請(qǐng)看 前端模板(web UI)

2018年5月18日 11:47