Advertisement
avaaren

lol

Apr 8th, 2020
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. def ajax_result(request):
  2.  
  3.     if request.method == 'POST':
  4.  
  5.         user = request.user
  6.         user_profile = get_object_or_404(Profile, user=user)
  7.         score = request.POST.get('result')
  8.         if score:
  9.             # Create object of game
  10.             GameSession.objects.create(
  11.                 user=user,
  12.                 score=score
  13.             )# Problem in get_object_or_404!!!!
  14.             # Update user info
  15.             user_profile.games_played = F('games_played') + 1
  16.             if int(score) > user_profile.high_score:
  17.                 user_profile.high_score = score
  18.             user_profile.save()
  19.             # Showing result
  20.        
  21.            
  22.             # redirect(reverse('result'))
  23.         return redirect(reverse('result'))
  24.  
  25. ### ОНО ВОЗВРАЩАЕТ РЕДИРЕКТ, И ГЕТ ЗАПРОС ПРИХОДИТ (
  26. [08/Apr/2020 11:15:56] "POST /ajax_result/ HTTP/1.1" 302 0
  27. [08/Apr/2020 11:15:56] "GET /result/ HTTP/1.1" 200 436
  28. )
  29. # НО ПОСЛЕ РЕДИРЕКТА НЕ РЕНДЕРИТСЯ ШАБЛОН, ХОТЯ ЕСЛИ РУКАМИ ВБИТЬ АДРЕС, ТО ШАБЛОН ВПОЛНЕ СЕБЕ РЕНДЕРИТСЯ
  30. class ResultView(TemplateView):
  31.  
  32.     template_name = 'game/result.html'
  33.     # def get(self, request):
  34.     #     return render(request, 'game/result.html',  {})
  35.  
  36.  
  37.     def get_context_data(self, **kwargs):
  38.         game_obj = GameSession.objects.filter(user=self.request.user).order_by('-time')[:1]
  39.         # high_scores = GameSession.objects.all().order_by('-score')[:5]
  40.         print(game_obj)
  41.         data = super().get_context_data(**kwargs)
  42.         data['game_result'] = game_obj[0]
  43.         # data['high_scores'] = high_scores
  44.         # data['your_place'] = your_place
  45.         return data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement