Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def ajax_result(request):
- if request.method == 'POST':
- user = request.user
- user_profile = get_object_or_404(Profile, user=user)
- score = request.POST.get('result')
- if score:
- # Create object of game
- GameSession.objects.create(
- user=user,
- score=score
- )# Problem in get_object_or_404!!!!
- # Update user info
- user_profile.games_played = F('games_played') + 1
- if int(score) > user_profile.high_score:
- user_profile.high_score = score
- user_profile.save()
- # Showing result
- # redirect(reverse('result'))
- return redirect(reverse('result'))
- ### ОНО ВОЗВРАЩАЕТ РЕДИРЕКТ, И ГЕТ ЗАПРОС ПРИХОДИТ (
- [08/Apr/2020 11:15:56] "POST /ajax_result/ HTTP/1.1" 302 0
- [08/Apr/2020 11:15:56] "GET /result/ HTTP/1.1" 200 436
- )
- # НО ПОСЛЕ РЕДИРЕКТА НЕ РЕНДЕРИТСЯ ШАБЛОН, ХОТЯ ЕСЛИ РУКАМИ ВБИТЬ АДРЕС, ТО ШАБЛОН ВПОЛНЕ СЕБЕ РЕНДЕРИТСЯ
- class ResultView(TemplateView):
- template_name = 'game/result.html'
- # def get(self, request):
- # return render(request, 'game/result.html', {})
- def get_context_data(self, **kwargs):
- game_obj = GameSession.objects.filter(user=self.request.user).order_by('-time')[:1]
- # high_scores = GameSession.objects.all().order_by('-score')[:5]
- print(game_obj)
- data = super().get_context_data(**kwargs)
- data['game_result'] = game_obj[0]
- # data['high_scores'] = high_scores
- # data['your_place'] = your_place
- return data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement