for example: multiple routing functions declare the global variable login, in global, and sometimes the following error occurs! There is to import variables from an app inside the function, the value has not changed!
NameError: name "login" is not defined
import base64
import time
from django.shortcuts import render
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from django.views.decorators.csrf import csrf_exempt
import requests
from requests.exceptions import ConnectionError
from common.log import logger
from mobile.mobile_login import UserLogin
from common.img_validate_code import RCaptcha
from common.mexceptions import StatusCodeError
from mobile.data_processing import AnalyseData
from common import user
@csrf_exempt
@api_view(["GET", "POST"])
@permission_classes((IsAuthenticated,))
def page2(request):
"""
:return:
"""
global login, phone, password, session
if request.method == "GET":
phone = user["phone"]
password = user["password"]
session = requests.session()
login = UserLogin(session, phone, password)
return render(request, "page2.html", {"message": "", "operator": "mobile"})
else:
try:
smspwd = request.POST.get("smspwd")
artifact = login.login(smspwd)
login.sso_login_shop(artifact)
result = {"code": "0", "message": ""}
except StatusCodeError as e:
result = {"code": "1", "message": "{}".format(e)}
except ConnectionError:
result = {"code": "2", "message": ","}
except Exception:
logger.exception("")
result = {"code": "4", "message": ""}
return Response(result)
def send_code01(request):
"""
:return:
"""
global login
try:
message = login.send_random_code_01()
except ConnectionError:
message = ","
except Exception:
logger.exception("")
message = ""
return render(request, "page2.html", {"message": message, "operator": "mobile"})
@csrf_exempt
@api_view(["GET", "POST"])
@permission_classes((IsAuthenticated,))
def page3(request):
"""
:return:
"""
global login, phone, password, session
if request.method == "GET":
return render(request, "page3.html", {"message": ",", "operator": "mobile"})
else:
try:
smspwd = request.POST.get("smspwd")
login.send_img_validate_code()
rk_captcha = RCaptcha("andrewuser", "Defender17&rk", "102055", "b05a3fe49ccb4adaaaa63b83a289ae01")
time.sleep(3)
img = login.read_img_validate_code()
rk = rk_captcha.rk_create(img, 3060)
captcha = rk["Result"]
login.check_img_validate_code(captcha)
params = {
"callback": login.callback,
"pwdTempSerCode": base64.b64encode(str(password).encode("utf-8")),
"pwdTempRandCode": base64.b64encode(str(smspwd).encode("utf-8")),
"captchaVal": captcha,
}
login.detail_login(params)
result = {"code": "0", "message": ""}
except StatusCodeError as e:
result = {"code": "1", "message": "{}".format(e)}
except ConnectionError:
result = {"code": "2", "message": ","}
except Exception:
logger.exception("")
result = {"code": "4", "message": ""}
return Response(result)
def send_code02(request):
"""
:return:
"""
global login
try:
login.send_random_code_02()
message = ""
except StatusCodeError as e:
message = "{}".format(e)
except ConnectionError:
message = ""
except Exception:
logger.exception("")
message = ""
return render(request, "page3.html", {"message": message, "operator": "mobile"})
@api_view(["GET", "POST"])
@permission_classes((IsAuthenticated,))
def page4(request):
"""
:return:
"""
global login, phone, password, session
try:
callback = login.callback
result = AnalyseData(session=session, phone=phone, callback=callback).get_full_info()
except StatusCodeError as e:
result = {"code": "1", "message": "{}".format(e)}
except ConnectionError:
result = {"code": "2", "message": ","}
except Exception:
logger.exception("")
result = {"code": "4", "message": ""}
return Response(result)