2016-11-29 40 views
1

之前引用局部变量“TOTO”我得到一个错误使用Django和我没有找到一个解决方案:UnboundLocalError:分配

UnboundLocalError在/身份/ RECHERCHE局部变量“TOTO” 分配

之前引用

这是我的脚本的一部分:

def Consultation(request) : 

    identity = Identity.objects.all().order_by("-id")[:10] #Les 10 dernières fiches créées 
    identity_France = Identity.objects.filter(country='64').order_by("-id")[:10] #Les 10 dernières fiches où la personne habite en France 

    query = request.GET.get('q') 
    if query : 
     toto = Identity.objects.filter(lastname__icontains=query)   

    context = { 
     "identity" : identity, 
     "identity_France" : identity_France, 
     "query" : query, 
     "toto" : toto, 
     } 

    return render(request, 'resume.html', context) 

太谢谢你了:)

+1

[托托(https://www.youtube.com/watch?v=FTQbiNvZqaY0)? –

+0

@DanielRoseman好笑话;)在法国,toto是常用变量名称:P – Deadpool

回答

1

正如消息所述,toto没有定义。

query = request.GET.get('q') 
if query : 
    toto = Identity.objects.filter(lastname__icontains=query)   
else : 
    toto = [] 

或许稍微更Python

try: 
    query = request.GET['q'] 
    toto = Identity.objects.filter(lastname__icontains=query)   
except KeyError: 
    toto = [] 
    query = None 
+0

很高兴能有所帮助 – e4c5

+0

作为一个初学者,有些东西只是逻辑,但这是我第一次做到这一点,所以很高兴受到人们的帮助;) – Deadpool

相关问题