views is like thiskwargs["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 itRandom 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
怪我咯2017-05-18 10:54:56
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 %}