我目前正在构建一个游戏应用程序,当前登录的用户可以根据他们在我们应用程序中的排名来玩不同的游戏.
因此,如果当前登录的用户的排名为100,则将显示游戏排名为100及以下的所有游戏.其余的比赛将被锁定.
我目前不能让它完全与我的代码一起工作.
我需要对代码进行哪些更改?
如果我使用When(game_rank__lte=25, then=Value(True)),
进行测试,似乎可以工作,我可以看到锁定和解锁的游戏,基于25和更小的值.
但当我做When(game_rank__lte=user_profile_rank, then=Value(True)),
次的时候,这似乎不起作用.所有的游戏都是解锁的,没有一个是锁定的.
每场比赛的排名是Game_Info
模型中的game_rank
场.
当前登录用户的排名是User_Info
模型中的rank
字段.
如有任何帮助,我们将非常感激.
谢谢!
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 %}