I Want to Build Your Website


Follow me here and let me show you what I can do!

Life is Random


Today I thought I would share with you something I did at 10beans.com to generate a random selection of shirts each time someone visited the index page for the site. Basically each time a visitor hits the index page and they are presented with a catalog of shirts, the catalog order changes, thus giving the illusion that the designs are constantly updated.

Making this happen was pretty easy, all I had to do was adjust my queryset inside my urls.py file:

Before I had this: 'queryset': Shirt.objects.all(), Now I have this: 'queryset': Shirt.objects.order_by('?'),

All that I had to do was add the order_by with a question mark, and Django did the rest. This isn't ground breaking, nor did I develop anything on my own, but knowing how to do this, especially for those whom are building product sites, should be able to display random objects to the end users.

Hope this helps, or at least cuts down on your searching for the answer on how to make your queryset random.

| More



Comments


No Response Yet

Why don't YOU kick it off by saying something ↓

Comment Form

Content is licensed under a Creative Commons Public Domain License