Created
December 19, 2021 00:56
-
-
Save antonioccneto/086ae0f9dca7db0cebe2256507d26844 to your computer and use it in GitHub Desktop.
Função simples pra testar download de arquivo no DRF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# viewsets.py | |
from rest_framework.decorators import api_view, permission_classes | |
from django.http import FileResponse | |
from django.shortcuts import get_object_or_404 | |
@api_view(['GET']) | |
@permission_classes((IsAuthenticated,)) | |
def download(file): | |
""" Função simples para Download de Arquivo """ | |
arquivo = file | |
obj = get_object_or_404(SeuModel, arquivo=arquivo) | |
return FileResponse(obj.arquivo) | |
# urls.py | |
from minhaApp.api.viewsets import download | |
... | |
path('media/<str:file>', download, name='download'), | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment