0
I want to do a random search in the database, but it’s not working. I want to take a sentence, and show it on the screen, it will be changed every time we update the screen.
My view.py:
def busca_tendencia(request):
tendencias = Frase.objects.all().order_by('?')
return render(request, 'busca/index.html', {'tendencias': tendencias})
In html:
{{ tendencias.frase }}
My models.py
class Frase(models.Model):
titulo = models.CharField(max_length=300, blank=False)
frase = models.TextField()
data = models.DateTimeField(default=timezone.now)
class Meta:
verbose_name = 'Frase'
verbose_name_plural = 'Frases'
When accessing the page does not return anything, nor error. And the phrase does not appear.
Possible duplicate of How to get random results in SQL with different Dbms?
– fernandosavio
I don’t think it’s duplicate @fernandosavio. Pedro failed to give the correct context that is of a Django application and not pure SQL, IE, he is using the ORM for this.
– André Duarte
You’re right @Andréduarte. I removed the flag, his doubt seems to me more like Queryset than with Andom itself. Thank you!
– fernandosavio