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

Viewed 59

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". I tried using 'import request' it says that 'No module named 'request''. This code is from my views.py that working well. Is it possible to use this also in a python file? or there is another way? I also add request to the def update_extend_traces_traceselect(request).

in my 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)
1 Answers

You forgot to add request to the function parameter.

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.

Related