【发布时间】:2022-02-05 14:55:21
【问题描述】:
我的model.py是这个。
class Country(models.Model):
country_name = models.CharField(max_length=200)
country_fact = models.CharField(max_length=1000)
country_capital = models.CharField(max_length=100)
country_flags = models.ImageField(upload_to='flags')
View.py 是这个
def index(request):
country = Country.objects.all()
return render(request,'index.html',{'country':country})
我正在使用这个在 HTML 中检索这些数据
{% for count in country %}
<img src="{{ count.country_flags.url }}">
这将从数据库中检索所有国家/地区图像(我使用的是 Postgresql)。我只想从数据库中检索一个随机的国家标志。我怎样才能做到这一点?感谢您的帮助。
【问题讨论】:
-
你可以使用
import randomrandom.choice(list)
标签: javascript python html django