#in youxi.urls.py
#...
#random gate
(r'^(?P<gameabbr>[a-z]+)/rand/$',
'fayaa.youxi.views.index.random_gate'),
#...
#in youxi.views.index.py
def random_gate(request, gameabbr):
game = Game.objects.get(abbr=gameabbr)
import sys
gatename = game.abbr[0].upper() + game.abbr[1:] + "Gate"
mods = sys.modules['fayaa.youxi.models']
gates = getattr(mods, gatename).objects
gate_count = gates.count()
import random
chance = 10
while chance:
chance -= 1
#NOTICE: we will lose some record if some are empty
# because that time gate_count != max_id
gate_id = random.randint(0, gate_count - 1)
if gates.filter(id=gate_id).count():
return HttpResponseRedirect("/youxi/" + gameabbr + "/" + str(gate_id) + "/")
return HttpResponseRedirect(".")