0

My model.py is this.

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 is this

def index(request):
    country = Country.objects.all()
    return render(request,'index.html',{'country':country})

I'm retrieving those data in HTML by using this

{% for count in country %}
 <img src="{{ count.country_flags.url }}">

This retrieves all the country images from the database(I'm using Postgresql). I want to retrieve only one random country flag from the database. How can I achieve this? Thanks for the help.

kiner_shah
  • 3,939
  • 7
  • 23
  • 37

1 Answers1

0
import random

def index(request):

    country = Country.objects.all()
    choosen = random.choice(country)


    context = {
        'country': country,
        'choosen': choosen
    }
    return render(request,'index.html',context)