0

I have a Django site that i'm managing for a client of mine that is around 5 years old. It has a very simple carousel on one of the pages that is content managed. THe client wants these images to be randomly picked on page load as opposed to the order in which they are organised in the CMS. Here is the code for the models for this element

 from django.db import models

 class Banner(models.Model):

 created = models.DateTimeField(auto_now_add=True)
 active = models.BooleanField(default=True, help_text='Display this banner on the website.')

 title = models.CharField(max_length=255)
 description = models.TextField()
 image = models.ImageField(upload_to='banners/')
 order = models.IntegerField(default=0)

 class Meta:
    ordering = ['order']

I was wondering if someone could help me by showing me how to do this or if it's even possible? This, in my opinion, is different from the suggested link as it does not show how to address this problem specifically.

Keith Riches
  • 63
  • 1
  • 8

2 Answers2

0

You can shuffle your query set by using

banner_obj = Banner.objects.all().order_by('?')

which will give you random result on every call

rahul.m
  • 5,572
  • 3
  • 23
  • 50
0

I decided to randomise things with js in the end as I didn't have time to work through an option with regards to the original query

Keith Riches
  • 63
  • 1
  • 8