我在使用 pylint
在我的代码上,得到错误的
R1710: 一个函数中的所有返回语句要么应该返回一个表达式,要么都不应该。(不一致的返回语句)
只有两种可能的返回语句,如果我没有弄错的话,它们都返回表达式。
@api_view(["GET", "POST"])
def user_info(request):
if request.method == 'GET':
username = request.GET.get("username")
password = request.GET.get("password")
return JsonResponse(error_handle(serialize(username, password)))
if request.method == 'POST':
username = request.data["username"]
password = request.data["password"]
return JsonResponse(error_handle(serialize(username, password)))
def error_handle(serializer):
error = serializer["error"].value
if error > 0:
return {"success": "false", "internal_code": error}
return {"success": "true",
"account_token": serializer.data["account_token"],
"user_id": serializer.data["id"],
"account_name": serializer.data["account_name"],
"account_permission": serializer.data["account_permission"],
"pin": serializer.data["pin"]
}
def serialize(user, password):
data = Account.objects.get(username=user, password=password)
return AccountSerializer(data)
我宁愿使用APIView来代替。
from rest_framework.response import Response
from rest_framework.views import APIView
class UserInfoView(APIView):
def get(self, request):
username = request.query_params.get('username')
password = request.query_params.get('password')
return Response(error_handle(serializer(username, password))
def post(self, request):
username = request.data["username"]
password = request.data["password"]
return Response(error_handle(serialize(username, password)))
会发生什么?user_info
如果 request.method
既不是 GET
也不 POST
?
@api_view(["GET", "POST"])
def user_info(request):
if request.method == 'GET':
# ...
return JsonResponse(error_handle(serialize(username, password)))
if request.method == 'POST':
# ..
return JsonResponse(error_handle(serialize(username, password)))
# ???
# Something should be returned here!