Django错误:语法无效(<string>,第1行)</string>

时间:2013-08-13 22:58:06

标签: django django-views

def play(request):
    if request.user.is_authenticated():
        number_of_records = Puzzles.objects.count()
        random_index = int(random.random()*number_of_records)+1
        rand_puzz = Puzzles.objects.get(id = random_index).puzzle
        solution = eval(rand_puzz)
        if 'a' in request.GET:
            a = request.GET['a']
                if a == request.GET['solution']:
                    message = "correct"
                    return render(request, 'play.html',                           {'rand_puzz':rand_puzz,'solution':solution,'message':message})
                else:
                    message = "incorrect"
                    return render(request, 'play.html', {'rand_puzz':rand_puzz,'solution':solution,'message':message})

        else:
            message = 'play the game'
            return render(request, 'play.html', {'rand_puzz':rand_puzz,'solution':solution,'message':message})
    else:
        return render_to_response('home.html')

所以我收到了这个错误:语法无效(第1行)。页面最初工作,然后会发生这种情况。我完全不知道这个错误意味着什么,任何帮助都会受到赞赏

1 个答案:

答案 0 :(得分:0)

找出导致异常的字符串。

 try:
      solution = eval(rand_puzz)
 except SyntaxError:
      print rand_puzz