2013-03-06 69 views
1

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

我测试从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 - 应用程序被称为名册。

回答

0

您还没有在这就是为什么你所得到的错误有

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没有定义外壳定义PK要么

tempteams = tempteams.players.all() 

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

NameError: name 'tempteams' is not defined