21번째 줄: |
21번째 줄: |
| return response | | return response |
| | | |
| + | </syntaxhighlight> |
| + | |
| + | === 더 나아가 === |
| + | 위 방식대로 하면 xls로 다운받아지고 에러메시지가 계속해 발생하고 파일명 설정이 먹히질 않는다. 아래 방식은 df를 엑셀로 다운받게 도와주는 함수를 모듈화 한 것인데, 참고하길.<syntaxhighlight lang="python"> |
| + | from django.http import HttpResponse |
| + | |
| + | def df_to_excel_download(df, filename='지난지나니 다운'): |
| + | response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') |
| + | from django.utils.encoding import escape_uri_path # 파일명을 '다운로드'가 아닌 지정한 형태로 가져가기 위해 필요한 것. |
| + | filename = f"{filename}.xlsx" |
| + | response['Content-Disposition'] = f'attachment; filename="{escape_uri_path(filename)}"' |
| + | df.to_excel(response, index=False) |
| + | return response |
| </syntaxhighlight> | | </syntaxhighlight> |
| [[분류:장고 엑셀]] | | [[분류:장고 엑셀]] |