Django - 如果用户是某个值显示元素

问题描述 投票:0回答:1

我目前正在构建一个游戏应用程序,当前登录的用户可以根据他们在我们应用程序中的排名来玩不同的游戏。

因此,如果当前登录的用户的排名为 100,则将显示排名 100 及以下的所有游戏。其余游戏将被锁定。

实现这一目标的最佳方法是什么? 如何根据登录用户的排名显示已解锁的游戏?

每场比赛的排名是

game_rank
模型中的
Game_Info
字段。

当前登录用户的排名是

rank
模型中的
User_Info
字段。

我当前的 HTML 一般显示哪些游戏被锁定和解锁,但没有显示特定于用户的内容。

代码如下。非常感谢任何帮助。

谢谢!

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})

home.html

            {% for content in user_profile_games %}
            {% if content.locked_game == True %}

            <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>

            {% else %}
            {% endif %}
            {% endif %}

            {% if content.unlocked_game == True %}

            <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>

            {% endif %}
            {% endif %}

            {% endfor %}

views.py

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

        
    if request.user.is_authenticated:
        user_profile = User_Info.objects.filter(user=request.user)
        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)
django django-models django-views django-templates
1个回答
0
投票

您可以根据用户的排名使用“unlocked_game”字段注释每个游戏,如下所示:

from django.db.models import Case, When, Value, BooleanField
user_profile_games = Game_Info.objects.annotate(
    unlocked_game=Case(
        When(game_rank__lte=user_profile_rank, then=Value(True)),
        default=Value(False),
        output_field=BooleanField()
    )
)

并在模板中使用如下所示:

{% if content.unlocked_game %}
<!-- unlocked games logic -->
{% else %}
<!-- locked games logic -->
{% endif %}
© www.soinside.com 2019 - 2024. All rights reserved.