Python代码: 游戏发芽网的随机关卡链接
01 #in youxi.urls.py
02 #...
03 #random gate
04 (r'^(?P<gameabbr>[a-z]+)/rand/$',
05 'fayaa.youxi.views.index.random_gate'),
06 #...
07
08 #in youxi.views.index.py
09 def random_gate(request, gameabbr):
10 game = Game.objects.get(abbr=gameabbr)
11 import sys
12 gatename = game.abbr[0].upper() + game.abbr[1:] + "Gate"
13 mods = sys.modules['fayaa.youxi.models']
14 gates = getattr(mods, gatename).objects
15 gate_count = gates.count()
16 import random
17 chance = 10
18 while chance:
19 chance -= 1
20 #NOTICE: we will lose some record if some are empty
21 # because that time gate_count != max_id
22 gate_id = random.randint(0, gate_count - 1)
23 if gates.filter(id=gate_id).count():
24 return HttpResponseRedirect("/youxi/" + gameabbr + "/" + str(gate_id) + "/")
25 return HttpResponseRedirect(".")