简体   繁体   English

如何将值/变量从一个 function 传递到 django 中的另一个 - 视图

[英]How to pass value/variable from one function to another in django - views

Not able to pass a variable from one function to another.无法将变量从一个 function 传递到另一个。

@api_view(['GET'])
def UserList(request):
    loc = User.objects.all()
    latest_id = User.objects.latest('userid').userid  #want to pass this variable into DetailList function
    serializer = UserSerializer(loc, many=True)
    return Response(serializer.data)

@api_view(['GET'])
def DetailList(request):
    loc = Detail.objects.all()
    serializer = DetailSerializer(loc, many=True)
    return Response(serializer.data)

and finaly want to use that userid variable to add the data into Detail table.最后想使用该用户标识变量将数据添加到详细信息表中。

class UserAPIView(APIView):
    def post(self, request ):
        userid = request.data.get('userid')
        name = request.data.get('name')
        age = request.data.get('age')
        gender = request.data.get('gender')
        address = request.data.get('address')
        phone = request.data.get('phone')
        user_serializer = UserSerializer(data={"name": name, "age": age, "gender": gender})
        detail_serializer = DetailSerializer(data={"userid": userid,"address": address, "phone": phone})
        if user_serializer.is_valid(raise_exception=True):
            user_serializer.save()
        if detail_serializer.is_valid(raise_exception=True):
            detail_serializer.save()
        return Response({'status': 'Success'})

not able to pass variable data through declaring global, as getting error Nulltype is not callable.无法通过声明全局来传递变量数据,因为出现错误 Nulltype 是不可调用的。

I don't know how to pass data from one view to another view, but I have an idea about the session, you can do same thing with session我不知道如何将数据从一个视图传递到另一个视图,但我对 session 有一个想法,你可以用 session 做同样的事情

#set session from view #set session 从视图

request.session['session_name'] = session_value

#get session from another view #从另一个角度获取 session

session_var = request.session.get('session_name')

Functions itself are stateles, in a meaning that each function have it's own context and context isn't shared between them.函数本身是状态,这意味着每个 function 都有自己的上下文,并且它们之间不共享上下文。 The only "bridge" you have, else then keep some data on frontend side, is to use DB or Django sessions as mentioned in previous answer.您拥有的唯一“桥梁”(否则在前端保留一些数据)是使用 DB 或Django 会话,如先前答案中所述。 For the last one here's an example:对于最后一个,这是一个示例:

@api_view(['GET'])
def UserList(request):
    loc = User.objects.all()
    latest_id = User.objects.latest('userid').userid  #want to pass this variable into DetailList function
    request.session['latest_id'] = latest_id  # here you set session var
    serializer = UserSerializer(loc, many=True)
    return Response(serializer.data)

@api_view(['GET'])
def DetailList(request):
    loc = Detail.objects.all()
    latest_id = request.session.get('latest_id')  # here you get session var
    serializer = DetailSerializer(loc, many=True)
    return Response(serializer.data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能从一个 function 中的另一个 function 中的 Django 中获取价值,views() - How can I get value from one function in another function in Django, views() 如何在 Django 中的另一个 function 中使用来自一个 function 的变量 - How to use a variable from one function in the other function in Django Views Django - 将值从模板传递给视图函数 - Django - pass value from template to views function 如何在一个 function 中将变量的值从一个 IF 传递到另一个 IF? - How do I pass the value of a variable from one IF to another, inside one function? 无法将变量值从一个函数传递给另一个函数 - Can't pass variable value from one function to another 如何将变量从一个 function 传递到 python 中的另一个 function - How to pass a variable from one function to another function in python 如何将变量从一个 function 传递到另一个 function - How to pass a variable from one function to another function 如何将变量从 Django views.py 传递到另一个 Python 文件? - how to pass a variable from the Django views.py to another Python file? 如何将字符串变量从一个 function 传递给另一个? - How to pass a string variable from one function to another? 如何将视图从一个django views.py导入到另一个 - How to import views from one django views.py to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM