Django中的NameError

时间:2013-03-06 22:30:19

标签: python-2.7 django-views

我正在使用Django / python并遇到一些错误,我似乎无法在网上找到相关的答案。

我在views.py中测试以下内容:

def teamRoster(request, pk): #shows list of players

    tempteams = Teams.objects.get(id=pk)
    tempteams = tempteams.players.all()
    team = get_object_or_404(Teams, id=pk)

    context = {
        'teamList': tempteams,
        'team': team,
    }

return render(request, "roster/teamRoster.html", context)

我在shell中得到了什么:

from roster.models import Teams, Player
tempteams=Teams.objects.get(id=pk)

Traceback (most recent call last):
File "<console>", line 1, in <module>

NameError: name 'pk' is not defined

如果我尝试下一行:

tempteams = tempteams.players.all()

Traceback (most recent call last):
File "<console>", line 1, in <module> 

NameError: name 'tempteams' is not defined

我的数据确实有一个pk属性,我在制作json文件时放入,而不是自动分配。我不确定我需要在哪里定义pk。我对django / python相当新,但是我确实做了类似于另一个定义(同一个项目)的东西,它运行正常。任何指导都将非常感谢。如果它有帮助,我的模型和代码的其他部分都在https://github.com/historymaiden/Athletic-Team-App - 该应用程序称为名册。

1 个答案:

答案 0 :(得分:0)

你还没有在shell中定义pk,这就是你在那里得到错误的原因

from roster.models import Teams, Player
tempteams=Teams.objects.get(id=pk)

Traceback (most recent call last):
File "<console>", line 1, in <module>

NameError: name 'pk' is not defined

因为最后一个代码块失败,所以也没有定义tempteams

tempteams = tempteams.players.all()

Traceback (most recent call last):
File "<console>", line 1, in <module> 

NameError: name 'tempteams' is not defined