Django.fun

I want to show each games title in its specific platform in the navbar

I want to show each games title in its specific platform in the navbar by creating a GameManager with a function called 'show_pc_game' which will only show games whose platform is PC.

This is my models.py:

class GameManager(models.Manager):
def show_pc_game(self):
    return self.filter(platform='PC')

class Platform(models.Model):
    PLATFORM_CHOICES = (
        ('PC', 'PC'),
        ('PS', 'playstation'),
        ('XBOX', 'Xbox'),
        ('Nintendo Switch', 'nintendo switch'),
    )
    title = models.CharField(max_length=15, choices=PLATFORM_CHOICES, verbose_name= 
    "platform")
    slug = models.SlugField(max_length=100, unique=True)

    class Meta:
        verbose_name = "platform"
        verbose_name_plural = "platforms"

    def __str__(self):
        return self.title

class Game(models.Model):
    title = models.CharField(max_length=50)
    description = models.TextField()
    slug = models.SlugField(max_length=100, unique=True)
    image = models.ImageField(upload_to="images")
    platform = models.ManyToManyField(Platform)

    class Meta:
        verbose_name = "game"
        verbose_name_plural = "games"

    def __str__(self):
        return self.title

    objects = GameManager()

my views.py:

def home(request):
    return render(request, 'blog/base.html')

class GameList(ListView):
    template_name = "blog/game.html"
    model = Game

class GameDetail(DetailView):
    template_name = "blog/game_detail.html"

class PlatformList(ListView):
    template_name = "blog/platform.html"
    model = Platform

I have used 'extends' in secondary html files so I have to apply changes only in 'base.html'

And this is my context_processors.py for apply contextes:

from .models import Platform, Game

def add_variable_to_context(request):
    return {
        'platforms': Platform.objects.all(),
        'games': Game.objects.all(),
        'PC_games': Game.objects.show_pc_game(),
    }

And my base.html:

    <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" data-bs-toggle="dropdown">Platform</a>
        <ul class="dropdown-menu">
            {% for platform in platforms %}
            <li><a class="dropdown-item" href="#"> {{platform.title}} &raquo; </a>
                {% for pcgame in PC_games %}
                <ul class="submenu dropdown-menu">
                    <li><a class="dropdown-item" href="#">{{game.title}}</a></li>
                </ul>
                {% endfor %}
            </li>
            {% endfor %}
        </ul>
    </li>

when I reload browser I get an error: 'Manager' object has no attribute 'show_pc_game'

any suggest to fix this?

Answers: 0