简体   繁体   English

如何在 django python 文件中使用 request.method?

[英]How can I use request.method in a django python file?

I'm trying to get the data from a POST but I can't use the 'request' it says "NameError: name 'request' is not defined".我正在尝试从 POST 获取数据,但我无法使用“请求”,它显示“NameError:名称‘请求’未定义”。 I tried using 'import request' it says that 'No module named 'request''.我尝试使用“导入请求”,它说“没有名为“请求”的模块”。 This code is from my views.py that working well.这段代码来自我的views.py,运行良好。 Is it possible to use this also in a python file?是否也可以在 python 文件中使用它? or there is another way?还是有另一种方法? I also add request to the def update_extend_traces_traceselect(request).我还将请求添加到 def update_extend_traces_traceselect(request)。

in my graph.py在我的graph.py中

 def update_extend_traces_traceselect(request):
    if request.method == 'POST':
        post_data = json.loads(request.body.decode("utf-8"))
        value = post_data.get('data')
        print(value)

You forgot to add request to the function parameter.您忘记向 function 参数添加request

def update_extend_traces_traceselect(request):
    if request.method == 'POST':
        post_data = json.loads(request.body.decode("utf-8"))
        value = post_data.get('data')
        print(value)

Read more about the Django request object and how to use it in function-based views .阅读有关Django 请求 object以及如何在基于函数的视图中使用它的更多信息。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM