1

I am developing a quiz application in Django. I wish to maintain a timer for attending a quiz, and user shouldn't be able to change the time from the front end. There maybe ways to implement one using JavaScript, but those could be easily changed by the user. So what i want is something like maintain a timer at the server side so that the quiz gets automatically submitted once the time is up. I am working on django 3.1. Please help.

Sruti
  • 31
  • 3

1 Answers1

1

You can store a timestamp value of when it will end in your .env file and on the frontend,

you call an api to the backend to get the timestamp and showing remaining time by timestamp of end time - now().

In the backend you would have a background tasks(you can use Celery) at specific time of each day to check if now() >= timestamp. If so the server will no longer accept upload from that user form

Linh Nguyen
  • 3,452
  • 4
  • 23
  • 67
  • Will this work if I want the timer to be different for different users? People may start the quiz at any time, and what I wish to have is enable each user to have their own time limit. Each user should be able to submit the quiz in a given period of the time limit. – Sruti Dec 12 '20 at 06:37
  • @Sruti that part is a little tricky if for each user, i suggest each user should have an `end_time()` column in database instead of using .env file. It's the only option i can think of without user messing with value on frontend – Linh Nguyen Dec 15 '20 at 06:57
  • Will something like django channels work? – Sruti Dec 15 '20 at 11:57
  • 1
    @Sruti i haven't work with django-channel yet but if i remember correctly it's for web socket but yeah real-time counter mightwork – Linh Nguyen Dec 16 '20 at 02:34