When trying to rate the game I get the error ‘GameDetail’ object has no attribute ‘object’. In the terminal I am pointed to the following lines:
return self.render_to_response(self.get_context_data(form=comment_form))
context = super().get_context_data(**kwargs)
views.py
class GameDetail(DetailView):
model = Game
context_object_name = 'game'
template_name = 'main/list_detail.html'
def post(self, request, **kwargs):
comment_form = NewCommentForm(request.POST)
if comment_form.is_valid():
user_comment = comment_form.save(commit=False)
pk = self.kwargs['pk']
game = self.object()
user_comment.game = game
user_comment.author = request.user
user_comment.save()
return redirect('game_detail', pk=pk)
if 'game_pk' in request.POST and 'rating' in request.POST:
game_pk = request.POST.get('game_pk')
rating = request.POST.get('rating')
game = self.object()
request.user.profile.rate_game(game, rating)
return redirect('game_detail', pk=game.pk)
return self.render_to_response(self.get_context_data(form=comment_form))
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
comments = Comment.objects.filter(game=self.object)
context['comments'] = comments
context['game'] = self.object
context['form'] = NewCommentForm
context['average_rating'] = self.object.get_average_rating
return context
HTML form where I put the rating:
<form method="POST" action="{% url 'game_detail' pk=game.pk %}">
{% csrf_token %}
<input type="hidden" name="pk" value="{{ game.pk }}">
<label for="rating">Rating:</label>
<select name="rating" id="rating">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<button type="submit">Submit</button>
</form>