2

I'm having trouble getting the JSON for function based views in django. I have the below code. I basically would like the function to return either json or an html page based on the user request.

@api_view(['GET'])
@renderer_classes((JSONRenderer,TemplateHTMLRenderer,BrowsableAPIRenderer))
def meld_live_orders(request):
    if request.method =='GET':
        current_orders = Meld_Sales.objects.values_list('TicketNo',flat=True).distinct()
        prev_orders = Meld_Order.objects.values_list('TicketNo',flat =True).distinct()

        live_orders = live_order_generator(current_orders,prev_orders)

        return render(request,'live_orders.html',{'live_orders':live_orders})

When i go to the url - http://localhost:8000/live-orders.json

I'm getting an error which states the below -meld_live_orders() got an unexpected keyword argument 'format'

Is this because i need to include the serializer class somewhere the same way in CBVs? Doesnt the @API_VIEW serialize the response?

i tried including format = '' in the function argument. but the problem is that it still renders html when i want it to render json.

2 Answers 2

6

You need to make some changes to your code.

Firstly, you need to use format_suffix_patterns in your urls if you have not defined it. This will allow us to use filename extensions on URLs thereby providing an endpoint for a given media type.

from rest_framework.urlpatterns import format_suffix_patterns

urlpatterns = [
     ...
]

urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'html']) # allow us to use '.json' and '.html' at the end of the url

Secondly. your view does not have a format parameter in the definition.

When using format_suffix_patterns, you must make sure to add the 'format' keyword argument to the corresponding views.

@api_view(['GET'])
@renderer_classes((JSONRenderer,TemplateHTMLRenderer,BrowsableAPIRenderer))
def meld_live_orders(request, format=None): # add a 'format' parameter
    ...

Thirdly, you need to return a DRF response and not a Django response which you are returning at the end of the view.

Sign up to request clarification or add additional context in comments.

Comments

0

You must have match a format parameter in the url pattern, but in the view function there is not an argument named format. Change the view definition into:

def meld_live_orders(request, format = ""):

6 Comments

I actually tried that .. sorry for skipping it in the question .. it still renders in html when i want it to render a json
Would you please present your url pattern?
Well i know, in the last line you have rendered a HTML page as the response, which should be a rest_framework.response.Response object.
is there a way i could have both? i.e the user gets json data only by including .json after the url? my url pattern - url(r'^live-orders$',meld_live_orders,name='live-orders')
Please lookup the document carefully. The correct way is to pass data of the serializer to the Response object. For JSONRenderer, the data will be converted into JSON format; for TemplateHTMLRenderer, the data will be pass to the template as a context variable.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.