Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
How to get only one random element from object?

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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

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)
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda