Django - 验证用户是否创建了一个对象
Django - Validating if user has created an object
我正在尝试创建高分列表。我需要同时拥有所有玩家的高分和当前玩家的高分。我用的是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>
...
您将 GameShopUser
对象与 auth.User
对象进行比较 - 它们永远不会相等。
改用current_user.gameshop_user == score['user'].user
。
current_user.gameshop_user.auth_user == score['user'].user
您在这里比较 contrib.auth.User 和 models.GameShopUser。尝试将 urrent_user.gameshop_user 与 score['user'].user
进行比较
我正在尝试创建高分列表。我需要同时拥有所有玩家的高分和当前玩家的高分。我用的是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>
...
您将 GameShopUser
对象与 auth.User
对象进行比较 - 它们永远不会相等。
改用current_user.gameshop_user == score['user'].user
。
current_user.gameshop_user.auth_user == score['user'].user
您在这里比较 contrib.auth.User 和 models.GameShopUser。尝试将 urrent_user.gameshop_user 与 score['user'].user
进行比较