def compare_hashes(request, i=0):
    i = int(i)
competitors = Competitors.objects.all()
DATABASE_ENGINE = settings.DATABASES['default']['ENGINE'].split('.')[-1]
if DATABASE_ENGINE == 'mysql':
    if not request.session.get('random_seed', False):
        ints = range(10000)
request.session['random_seed'] = sample(ints, 1)[0]
seed = request.session['random_seed']
competitors = competitors.extra(select={'sort_key': 'RAND({})'.format(seed)})
randomized_competitors = competitors.order_by('sort_key')
try:
    chosen_competitor = randomized_competitors[i]
except IndexError:
    return render(request, 'hash_to_hash.html', {'tag1': '', 'tag2': '', i:
        0, 'done': True})
if request.method == 'POST':
    form = CompetitorForm(request.POST)
if form.is_valid():
    if 'yes' in request.POST:
        competitors.votes += 1
competitors.save()
i += 1
return render(request, 'hash_to_hash.html', {'tag1': chosen_competitor.tag1,
    'tag2': chosen_competitor.tag2, 'action': '/hash/{}'.format(i), 'done':
    False})
Randomize a Django queryset once, then iterate through it