-1

I want to create a list of questions and from it I want to ask random 5 questions to each logged in user.How can i do it. where can I create a list of quetions and options.How can I render questions and answer to html page ?

I tried to create a db table and used it to get values in html template But it did not work.

class Quetions(models.Model):
    quetion =models.CharField(max_length=100,default='')
    option1=models.CharField(max_length=20,default='')
    option2=models.CharField(max_length=20,default='')
    option3=models.CharField(max_length=20,default='')
    option4 =models.CharField(max_length=20, default='')
mplungjan
  • 169,008
  • 28
  • 173
  • 236
omkar more
  • 105
  • 2
  • 10
  • The django tutorial walks you through how to build a poll application. This is basically what you are looking for. I suggest doing the tutorial then if/when you have a problem with the randomization, come back with your question. https://docs.djangoproject.com/en/3.0/intro/tutorial01/ – monkut May 12 '20 at 08:14

1 Answers1

0

While fetching the question, you can do something like

pks = Quetions.objects.values_list('pk', flat=True)
random_idx = random.sample(range(0, len(pks)-1), 5)
random_questions = Quetions.objects.filter(pk__in=random_idx)
webbyfox
  • 1,049
  • 10
  • 22