[Django]-Serve random image with Django

2👍

As commented ImageField doesn’t actually store images in database, it has links to images in filesystem and generates proper url to image for it’s use in templates.

You can get random image using rImage = Images.objects.order_by('?')[0] but keep in mind that it can be expensive and slow operation depending on database backend you’re using (e.g. mysql). Then you can pass image object to template and use {{rImage.image_field.url}} (image field handles url generation for you, just set proper values in your project settings)

Leave a comment