Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django-验证用户是否创建了对象_Python_Django - Fatal编程技术网

Python Django-验证用户是否创建了对象

Python Django-验证用户是否创建了对象,python,django,Python,Django,我正在尝试创建一个高分列表。我需要所有球员的高分和当前球员的高分。我使用django auth 由于某些原因,我无法检查当前用户是否拥有游戏。 if current\u user.gameshop\u user.auth\u user==score['user']。user:-->即使current\u user.gameshop\u user.auth\u user-->玩家一号和玩家一号。user:-->玩家一号 型号.py ... class GameShopUser(models.Mod

我正在尝试创建一个高分列表。我需要所有球员的高分和当前球员的高分。我使用django auth

由于某些原因,我无法检查当前用户是否拥有游戏。
if current\u user.gameshop\u user.auth\u user==score['user']。user:
-->即使
current\u user.gameshop\u user.auth\u user
-->玩家一号和
玩家一号。user:
-->玩家一号

型号.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')
...
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
...
view.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')
...
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>
...
。。。
高分
{高分数%中的分数输入为%}
{{forloop.counter}}
{{scoreEntry.user}
{{scoreEntry.score}
{%endfor%}
我的高分
{{top.index}}
{{top.user.user}
{{top.score}}
...

您将一个
GameShopUser
对象与一个
auth.User
对象进行比较-它们永远不会相等


使用
current_user.gameshop_user==score['user']。用户

您正在比较一个
GameShopUser
对象和一个
auth.User
对象-它们永远不会相等

current_user.gameshop_user.auth_user == score['user'].user
使用
current_user.gameshop_user==score['user']。用户

current_user.gameshop_user.auth_user == score['user'].user
这里您将比较contrib.auth.User和models.GameShopUser。尝试将当前的\u user.gameshop\u用户与分数['user'].user进行比较

这里您将比较contrib.auth.User和models.GameShopUser。尝试将当前的\u user.gameshop\u用户与分数['user'].user进行比较