我正在尝试创建一个高分列表。我需要得到所有球员的高分和当前球员的高分。我用django auth。
由于某种原因,我无法检查当前用户是否拥有游戏。
if current_user.gameshop_user.auth_user == score['user'].user:
- >即使current_user.gameshop_user.auth_user
- >也是假的Player_one和score['user'].user:
- > Player_one。
models.py
...
class GameShopUser(models.Model):
auth_user = models.OneToOneField(User, related_name='gameshop_user')
email_validated = models.BooleanField(default=False)
class ScoreEntry(models.Model):
timestamp = models.DateTimeField('date scored')
score = models.IntegerField()
user = models.ForeignKey('GameShopUser', related_name='scores')
game = models.ForeignKey('Game', related_name='scores')
view.py
...
def play_view(request, slug):
current_user = request.user
queryset = ScoreEntry.objects.order_by('-score')
high_scores = queryset[:3]
all_scores_without_index = queryset.values()
all_scores = []
i = 0
for score in all_scores_without_index:
i += 1
all_scores.append(score)
score['index'] = i
score['user'] = ScoreEntry.objects.get(id=score['id'])
high_scores_of_current_player = []
for score in all_scores:
if current_user.gameshop_user.auth_user == score['user'].user:
print("success")
print(current_user)
print(score['user'].user)
top = {}
top_score = 0
for score in all_scores:
if score['score'] > top_score:
top_score = score['score']
top = score
...
play.html
...
<h4>High Scores</h4>
<table class="table">
{% for scoreEntry in high_scores %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ scoreEntry.user }}</td>
<td>{{ scoreEntry.score }}</td>
</tr>
{% endfor %}
</table>
<h4>My High Scores</h4>
<table class="table">
<tr>
<td>{{ top.index }}</td>
<td>{{ top.user.user }}</td>
<td>{{ top.score }}</td>
</tr>
</table>
</div>
...
答案 0 :(得分:4)
current_user.gameshop_user.auth_user == score['user'].user
这里您将contrib.auth.User与models.GameShopUser进行比较。尝试将urrent_user.gameshop_user与得分[&#39;用户&#39;]进行比较。用户
答案 1 :(得分:3)
您将GameShopUser
对象与auth.User
对象进行比较 - 这些对象永远不会相等。
请改用current_user.gameshop_user == score['user'].user
。