python - How to get random data every time django calls a tag
大家讲道理
大家讲道理 2017-05-18 10:52:56
0
3
737
The code for

views is like this
kwargs["Random Paragraph"] = Content.objects.order_by("?")[0].content

Is to randomly extract a paragraph each time
Then the index is like this

{{ 随机段落 }}
{{ 随机段落 }}
{{ 随机段落 }}

My purpose is to do this every time I call it
Random paragraph 1 Random paragraph 2 Random paragraph 3

To put it simply, every time the random paragraph tag is called, go to the database to extract the data

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(3)
刘奇

I don’t know if I understand your question.

According to my understanding, you need:

# view
# 取出多个随机元素
# 元素个数不足什么的请自己注意处理
kwargs["paragraphs"] = Content.objects.order_by("?")[:3].content
{# 模版 #}
{# 在循环过程中分别渲染各个“随机段落” #}
{% for paragraph in paragraphs %}
    {{ paragraph }}
{% endfor %}
PHPzhong

Write a random function

洪涛

kwargs["随机段落"] = lambda: Content.objects.order_by("?")[0].content

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!