In Django, how to temporatily store consecutive user inputs in the backend within 60s?

Viewed 33

I am having trouble storing user inputs temporarily in Django backend (like 60 seconds for a game. Once the game is over, it should delete all user input records).

To make it clear:

I want to develop a website game using python and Django as backend languages, where it takes user input (one word as one input) and counts how many words a user can give in 60 seconds. The website should remain the same after each submission, but the game records each user input in the backend and counts on it while the game is still running. I think that I should use a session in Django but I'm still not sure about how should I approach this problem.

As the picture shows, the input box can only take 1 word for each submission, and continue taking user inputs until the timer ends. So, how should I save these temporary user inputs using Django?

1 Answers
import json
from django.core.cache import cache


def update(userid, new_word, ttl=300):
    key = f'{userid}_words'
    words = json.loads(cache.get(key, '[]'))
    words.append(new_word)
    cache.set(key, json.dumps(words), ttl)

Or just let django pickle the list for you if you find the json calls tedious. (I like to keep the serialized form simple; YMMV.)

Related