Search code examples
djangodjango-modelsdjango-viewsdjango-templates

Django - Cannot Display Elements Based On Field Value


I’m currently building a gaming app where a currently logged in user has access to play different games based on their rank in our application.

So if a currently logged in user has a rank of let’s say 100 than all the games with a game rank of 100 and below will be displayed. The rest of the games will be locked.

I can’t get this currently fully to work with my code.

What do I need to change with my code?

If I test using When(game_rank__lte=25, then=Value(True)), that seems to work, I can see both locked and unlocked games based on the value of 25 and less.

But when I do When(game_rank__lte=user_profile_rank, then=Value(True)), that doesn’t seem to work. All the games are unlocked and none are locked.

The rank of each game is the game_rank field in the Game_Info model.

The rank of the currently logged in user is the rank field in the User_Info model.

Any help is gladly appreciated.

Thanks!

models.py

class Game_Info(models.Model):
    id = models.IntegerField(primary_key=True, unique=True, blank=True, editable=False)
    game_title = models.CharField(max_length=100, null=True)
    game_rank = models.CharField(max_length=1000, null=True, blank=True)
    game_image = models.ImageField(default='default.png', upload_to='game_covers', null=True, blank=True)
    locked_game = models.BooleanField(default=0)
    unlocked_game = models.BooleanField(default=0)

class User_Info(models.Model):
    id = models.IntegerField(primary_key=True, blank=True)
    image = models.ImageField(default='/profile_pics/default.png', upload_to='profile_pics', null=True, blank=True)
    user = models.OneToOneField(settings.AUTH_USER_MODEL,blank=True, null=True, on_delete=models.CASCADE)
    rank = models.CharField(max_length=100, null=True, blank=True, default=1)    
    user_games_enabled = models.ManyToManyField(Game_Info, blank=True, limit_choices_to={'unlocked_game': True})

views.py

from django.db.models import Case, When, Value, BooleanField, F, IntegerField
from django.db.models.functions import Cast

def account_view_home(request):
    user_profile = User_Info.objects.all()
    user_profile_rank = User_Info.rank
    user_profile_games = Game_Info.objects.all()
    user = request.user


    if request.user.is_authenticated:
        user_profile = User_Info.objects.filter(user=request.user)
        user_profile_rank = int(user_profile.rank)  


        user_profile_games = Game_Info.objects.annotate(
            user_unlocked_game=Case(
            When(game_rank__lte=Cast('game_rank', output_field=IntegerField()), then=Value(True)),
            default=Value(False),
            output_field=BooleanField()
            )
        )


        context = {
            'user_profile': user_profile,
            'user_profile_games' : user_profile_games,
        }

    else:
        context = {
            'user_profile': user_profile,
            'user_profile_games' : user_profile_games
       }

    return render(request, 'home.html', context)

home.html

                {% for content in user_profile_games %}
                {% if content.user_unlocked_game %}
                <!-- unlocked games logic -->

                <a class="game-tile-container" href="{% url 'detail' content.pk %}">
                    <div class="image-wrapper" style="padding-right: 5px;">
                        <span class="material-icons play_btn">play_circle_filled</span>
                        <img class="game-art" src="{{content.game_image.url }}" />
                    </div>
                </a>
                <p class="game-title">{{ content.game_title }}</p>

                {% else %}
                <!-- locked games logic -->

                <a class="game-tile-container" href="{% url 'detail' content.pk %}">
                    <div class="locked_game" style="padding-right: 5px;">
                        <div class="image-wrapper">
                            <img class="game-art" src="{{content.game_image.url }}" /> 
                            <img class="lock-img" src={% static 'images/treasure-chest-closed-alt.png' %} />
                            <button class="level-up">Reach level {{ content.game_rank }} to unlock</button>
                        </div>
                    </div>
                </a>
                <p class="game-title">{{ content.game_title }}</p>

                {% endif %}
                {% endfor %}

Solution

  • Your approach of comparing the rank is conceptually correct but the problem you are facing is because of the following two reasons:

    1. You are referring to the rank field of the User_Info model and not the instance of User_Info

    FOllowing is the problematic part:

    user_profile_rank = User_Info.rank
    
    1. You are not casting the CharField fields to integer before comparison

    In your views, make the following changes:

    if request.user.is_authenticated:
        user_profile = User_Info.objects.get(user=request.user)
        user_profile_rank = int(user_profile.rank)  # Convert rank to integer
    
       
        user_profile_games = Game_Info.objects.annotate(
            user_unlocked_game=Case(
                When(game_rank__lte=Cast('game_rank', output_field=IntegerField()), then=Value(True)),
                default=Value(False),
                output_field=BooleanField()
            )
        )
    

    I hope it will solve the problem.