,大家下午好之前引用局部變量,Django的背景:分配
我在我Django project
開始有點問題,我的環境變量。我有一個讓我能夠完成某些過程的功能。特別是對我數據庫的一些研究並顯示結果。
但是我有一個變量的一個問題:folderId
我得到這個錯誤,我沒有找到一個方法來克服它:
Environment:
Request Method: GET
Request URL: http://localhost:8000/Identity/recherche
Django Version: 1.10.3
Python Version: 2.7.12
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'BirthCertificate',
'Identity',
'bootstrapform',
'Accueil',
'captcha',
'django_countries',
'log',
'Mairie',
'Table',
'Recensement']
Installed Middleware:
['django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.gzip.GZipMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware']
Traceback:
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _legacy_get_response
249. response = self._get_response(request)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)
File "/Users/valentinjungbluth/Desktop/Django/Etat_civil/Identity/views.py" in Identity_Researching
121. "folderId" : folderId,
Exception Type: UnboundLocalError at /Identity/recherche
Exception Value: local variable 'folderId' referenced before assignment
我的功能看起來像:
@login_required
def Identity_Researching(request) :
#######################
# Display some arrays #
#######################
identitys = Identity.objects.order_by("-id")
identity = identitys[:3] #The 3 last created forms
identity_France = identitys.filter(country=64)[:3] #The 3 last created form with BirthCity = France
############################
# People searching process #
############################
query_lastname = request.GET.get('q1')
query_firstname = request.GET.get('q1bis')
query_birthday = request.GET.get('q1ter')
if query_lastname or query_firstname or query_birthday :
query_lastname_list = Identity.objects.filter(lastname__contains=query_lastname, firstname__contains=query_firstname, birthday__contains=query_birthday)
title = str(query_lastname + "_" + query_firstname + "_" + query_birthday)
##########################################
# Look if people directory already exist #
##########################################
url = 'http://demodoc/services/rest/folder/listChildren?folderId=8552450'
payload = {'folderId': 8552450}
headers = {'Accept': 'application/json'}
r = requests.get(url, params=payload, headers=headers, auth=('etatcivil', '100%EC67'))
rbody = r.content
data = json.loads(rbody)
longueur = len(data)
i=0
list = []
while i < longueur :
if data[i]["name"] == title :
list = [data[i]]
i = i+1
############################################
# We have correspondance, use ID directory #
############################################
if len(list) == 1 :
folderId = list[0]["id"]
else :
return HttpResponseRedirect('searched')
else :
query_lastname_list = Identity.objects.none() # == []
if "Retour" in request.POST :
return HttpResponseRedirect('accueil')
context = {
"identity" : identity,
"identity_France" : identity_France,
"query_lastname" : query_lastname,
"query_firstname" : query_firstname,
"query_birthday" : query_birthday,
"query_lastname_list" : query_lastname_list,
"folderId" : folderId,
}
return render(request, 'resume.html', context)
我錯過了什麼或訂單不正確?當我有這樣那樣的錯誤,我也不是很明白我有什麼爲了克服它做..
謝謝你的任何幫助:)
當你的if語句返回false時,你想要發生什麼? ('如果query_lastname或query_firstname或query_birthday') – Sayse