How to solve .accepted_renderer not set on Response Error in Django

Viewed 3232

I'm new at django and I have to use it in my project. So, team mates create micro services using Docker container. I have to call these micro services to execute the text written in text field. To do that I wrote a views.py file but when I try to write a sentence and call these micro services it gave me a AssertionError .accepted_renderer not set on Response error.

views.py

def link1(request):
if request.method == "POST":
            url = 'http://localhost:5000/sentiment/'
            payload = {'text':request.POST.get("response")}
            response = json.dumps(requests.post(url, data = payload).text)
            return Response (response)     
    return render(request, 'blog/links/Link1.html')

Link1.py

<form class="text" method="POST"action="{% url 'duyguanalizi' %}">
                        {% csrf_token %}
                        <label for="textarea"> 
                            <i class="fas fa-pencil-alt prefix"></i> Duygu Analizi 
                        </label> 
                        <h2>Kendi Metniniz ile Test Edin...</h2>
                        <input class="input" id="textarea" type="text" name="text">
                        </input>
                        <button type="submit" class="btn" name="submit" onclick="submitInfo()" >Dene</button>
                     </form>
                     {% if input_text %}
                     <label >Sonuç </label>
                     <p name ="input_text" id="input_text"><strong>{{ response }}</p>
                     {% endif %}

This is my full error:

Request Method: POST Request URL: http://127.0.0.1:8000/duyguanalizi/ Django Version: 3.0.5 Exception Type: AssertionError Exception Value: .accepted_renderer not set on Response Exception Location: C:\Users\Asus\AppData\Local\Programs\Python\Python39\lib\site-packages\rest_framework\response.py in rendered_content, line 55 Python Executable: C:\Users\Asus\AppData\Local\Programs\Python\Python39\python.exe Python Version: 3.9.1

2 Answers

You need to set the @api_view(['POST']) decorator.

@api_view(['POST'])
def link1(request):
    ...

Also, you don't need json.dumps(response), there is response.json().

Always use an APIView class or @api_view function decorators for views that return Response objects. Doing so ensures that the view can perform content negotiation and select the appropriate renderer for the response, before it is returned from the view.

@api_view(['POST'])
def link1(request):
Related