'GameDetail' object has no attribute 'object'

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)


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

            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>
    <button type="submit">Submit</button>

When requestion assistance with an error in the future, please post the complete error with the traceback that you are getting.

It appears that the problem is here:

You don’t have an attribute named object in this object.

I tried to write Comment.objects.all() instead of Comment.objects.filter(game=self.object)
The error still remains.


Traceback (most recent call last):
  File "C:\Users\masad\PycharmProjects\rewr\.venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "C:\Users\masad\PycharmProjects\rewr\.venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\masad\PycharmProjects\rewr\.venv\lib\site-packages\django\views\generic\base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\masad\PycharmProjects\rewr\.venv\lib\site-packages\django\views\generic\base.py", line 143, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\masad\PycharmProjects\rewr\main\views.py", line 43, in post
    return self.render_to_response(self.get_context_data(form=comment_form))
  File "C:\Users\masad\PycharmProjects\rewr\main\views.py", line 46, in get_context_data
    context = super().get_context_data(**kwargs)
  File "C:\Users\masad\PycharmProjects\rewr\.venv\lib\site-packages\django\views\generic\detail.py", line 95, in get_context_data
    if self.object:
AttributeError: 'GameDetail' object has no attribute 'object'
[04/Jun/2024 12:40:33] "POST /game_detail/2/ HTTP/1.1" 500 84531

This makes it clear to me what’s happening:

The basic issue here is that because you’re inheriting from a DetailView, you’re needing to handle the post method yourself. This means you need to set self.object in your post method - Django’s not going to do that for you.

I made a few more changes and finally everything worked. But how can I figure out what the error is from the “if self.object:” line?

The source for that line is:

It was seeing that the error was in detail.py that made me look more closely at your post method. I didn’t take particular note that you inherited from DetailView at first. Since I know that DetailView doesn’t natively handle a post request, that’s what made me look more closely at your post method, and I saw where you weren’t setting self.object. (It’s handled in the get method by the line self.object = self.get_object().)