ilayda
ilayda

Reputation: 53

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

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

Upvotes: 1

Views: 7233

Answers (3)

р&#252;ффп
р&#252;ффп

Reputation: 5448

In my case I wanted to override the default error handler and got the same error when returning a Response (from rest_framework.response).

The solution was to finally use a JSONResponse object instead like in this example:

from django.http import JsonResponse

def json_response_handler404(request, exception=None):
    data = {'status': 404, 'message': "Not Found"}
    return JsonResponse(data=data, status=404)

I know this is not exactly 100% as the OP's request but I think that solution can also be useful for people interested by overriding the default error handlers.

Upvotes: 0

Rajesh
Rajesh

Reputation: 1

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):

Upvotes: 0

Simeon Aleksov
Simeon Aleksov

Reputation: 1325

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().

Upvotes: 3

Related Questions