Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to return a json Response when query_params with 'format=xlsx' #38

Open
wgf4242 opened this issue Jun 23, 2019 · 2 comments
Open

how to return a json Response when query_params with 'format=xlsx' #38

wgf4242 opened this issue Jun 23, 2019 · 2 comments

Comments

@wgf4242
Copy link

wgf4242 commented Jun 23, 2019

If there isn't a 'form' param in request ,I want return an error, but raised an error.

def list(self, request: Request, *args: Any, **kwargs: Any) -> Response:
    if self.request.query_params.get('form') and self.request.query_params.get('format') == 'xlsx':
        return Response({'error' : 'no form'})

error:

Response data is a dict, not a DataFrame!

@wgf4242 wgf4242 changed the title how to return a normal Response if with format=xlsx how to return a normal Response when query_params with 'format=xlsx' Jun 23, 2019
@wgf4242 wgf4242 changed the title how to return a normal Response when query_params with 'format=xlsx' how to return a json Response when query_params with 'format=xlsx' Jun 23, 2019
@sheppard
Copy link
Member

Yes, by default the Response object is rendered with the negotiated renderer, regardless of the content. Here are some things you could try:

  1. Throw an exception instead of returning a Response
  2. Explicitly override the "Content-Type" header to be "application/json" (perhaps using Django's HttpResponse)
  3. Generate a small DataFrame with a single row containing the error message (pd.DataFrame([{"error": no form"}])

@thmxluis
Copy link

image

Hello, could you help me? I am trying to generate an excel and everything is fine but it is not downloaded, it is only generated in the browser or does anyone have any idea why?

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants