0

I want to pull random images from database with there id number This is my models.py code and Here i am making id field , name and image field

from django.db import models
from django.db import models
from os import listdir
import os.path
from django.conf import settings
from os.path import isfile
from os.path import join as path_join
from random import choice

import internship.settings


def random_img():
    dir_path = os.path.join(internship.settings.BASE_DIR, 'media')
    files = [
        content for content in listdir(dir_path)
        if isfile(path_join(dir_path, content))
    ]
    return choice(files)


# Create your models here.
class images(models.Model):
    id_no = models.IntegerField()
    name = models.CharField(max_length=20)

    image = models.ImageField(upload_to='images')

so i want to pull out and show it in a template but everytime i reload the page i need to show a new images .. So How can i do that? i cant able to get the logic

hackaman79
  • 19
  • 5

0 Answers0