How do I access a POST endpoint from a Django view?

Asked

Viewed 124 times

1

I need to provide a service that requests a Django view method using lib requests. The method is as follows::

def classify(request):                                    

    print(request)

    if request.method == "POST":                          
        form = DocumentForm(request.POST, request.FILES)  

        if form.is_valid():                               
            clf = Classifying()                           
            file = request.FILES['file']                  
            file_name = str(file.name)                    
            predict = str(clf.predict(file))                                      

            response = {                                      
                'predict': predict,                           
                'document': file_name,                        
                'run': True                                   
            }                                                 

         data = json.dumps(response, cls=DjangoJSONEncoder)
         return HttpResponse(data)                         

return HttpResponse({})

At the moment, the method captures a request file, but this will be modified later. The objective of this method is to take a text, classify and return the classification (1 or 0, in this case). I made a simple form with html to test this method and it works. But when I used lib requests did not work. This method is not even called (I put the print above to check if it would at least print the content of the request). Code:

r = requests.post('http://127.0.0.1:8000/classify', data={'text': 'Excelentissimo Juíz...'})
print(r.url)

The answer from the print is Response [500]. How can I fix this?

  • If the answer code is 500, just open the error log of the server you are using and see what the error is.

  • And where is the code that Voce developed for the endpoint what you used for this?

  • Apparently you’re making a mess of the concept of endpoint, i reformulated my answer, read it in full that you probably have the answer to that new question too.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.