from django.views.generic import TemplateView

from competition.models import Competition


class HomePageView(TemplateView):
    template_name = "home/home.html"
    
    def get_context_data(self, **kwargs):
      context = super(HomePageView, self).get_context_data(**kwargs)
      my_competitions = Competition.objects.user_registered(self.request.user)
      context["registered_competitions"] = my_competitions.exclude(is_running=False, is_open=False)
      context["closed_competitions"] = my_competitions.filter(is_running=False, is_open=False)
      return context


from django.views.generic import TemplateView

from competition.models import Competition


class HomePageView(TemplateView):
    template_name = "home/home.html"
    
    def get_context_data(self, **kwargs):
        context = super(HomePageView, self).get_context_data(**kwargs)

        if not self.request.user.is_anonymous():
            my_competitions = Competition.objects.user_registered(self.request.user)
            context["registered_competitions"] = my_competitions.exclude(is_running=False, is_open=False)
            context["closed_competitions"] = my_competitions.filter(is_running=False, is_open=False)
        return context


Check if user is not anonymous on homepage
Check if user is not anonymous on homepage

  from django.views.generic import TemplateView
  
  from competition.models import Competition
  
  
  class HomePageView(TemplateView):
      template_name = "home/home.html"
      
      def get_context_data(self, **kwargs):
-       context = super(HomePageView, self).get_context_data(**kwargs)
+         context = super(HomePageView, self).get_context_data(**kwargs)
+ 
+         if not self.request.user.is_anonymous():
-       my_competitions = Competition.objects.user_registered(self.request.user)
+             my_competitions = Competition.objects.user_registered(self.request.user)
-       context["registered_competitions"] = my_competitions.exclude(is_running=False, is_open=False)
+             context["registered_competitions"] = my_competitions.exclude(is_running=False, is_open=False)
-       context["closed_competitions"] = my_competitions.filter(is_running=False, is_open=False)
+             context["closed_competitions"] = my_competitions.filter(is_running=False, is_open=False)
-       return context
+         return context
  
  
Check if user is not anonymous on homepage
## Code Before:
from django.views.generic import TemplateView

from competition.models import Competition


class HomePageView(TemplateView):
    template_name = "home/home.html"
    
    def get_context_data(self, **kwargs):
      context = super(HomePageView, self).get_context_data(**kwargs)
      my_competitions = Competition.objects.user_registered(self.request.user)
      context["registered_competitions"] = my_competitions.exclude(is_running=False, is_open=False)
      context["closed_competitions"] = my_competitions.filter(is_running=False, is_open=False)
      return context


## Instruction:
Check if user is not anonymous on homepage
## Code After:
from django.views.generic import TemplateView

from competition.models import Competition


class HomePageView(TemplateView):
    template_name = "home/home.html"
    
    def get_context_data(self, **kwargs):
        context = super(HomePageView, self).get_context_data(**kwargs)

        if not self.request.user.is_anonymous():
            my_competitions = Competition.objects.user_registered(self.request.user)
            context["registered_competitions"] = my_competitions.exclude(is_running=False, is_open=False)
            context["closed_competitions"] = my_competitions.filter(is_running=False, is_open=False)
        return context

