mardi 14 juin 2016

Python-Django: save seen items in order to not show them again?

I have a site that gives you random jokes, and the problem is that sometimes it shows the same joke too often. I have a Joke model, and I want to save the ids of these jokes and for every visitor make sure that the same joke is not shown for, let's say, 20 rounds. But how can I save such data and pass them around? Here are my views:

def random_good_joke_page(request):
    """If asked for a random joke, redirect to joke's page."""
    return redirect('/jokes/%d/' % Joke.find_joke("Hot").id)


def random_cold_joke_page(request):
    """If asked for a random joke, redirect to joke's page."""
    return redirect('/jokes/%d/' % Joke.find_joke("Cold").id)

Now, I need to save the ids of the jokes already shown and check the new id (given from the find_joke function, that takes a joke rating and returns a random appropriate joke), and if that joke was shown recently, pick a new id, until there is one not yet shown. How can I save these data and pass them to Django? I thought of cookies, or maybe add such functionality to the Joke model(although I don't think that would be for each visitor, but for all of them...). Is there some default practical way for such situations? Thank you

Aucun commentaire:

Enregistrer un commentaire