I have a website where we download a xl file i have my view as below instead of returning the excel file its returning the text file with name of the object inside it how do i solve this
views.py:
def my_view(request):
obj = model.objects.first()
response = HttpResponse(file, content_type='
application/vnd.ms-excel',
)
return response
urls.py:
path('temo/fill',views.my_view,name = 'my-view')
models.py
class Model(BaseModel, SingletonModel):
file = models.FileField(
upload_to='',
validators=[FileExtensionValidator([''])]
)
person_uploaded = models.ForeignKey(
'somemodel',
related_name='s',
null=True,
on_delete=models.SET_NULL,
)
CodePudding user response:
obj = Plate.objects.first() # this will return you object
in objects there are multiple attributes there you've to get only your file attribute like this
@api_view(['GET',])
def my_view(request):
obj = Plate.objects.first()
response = HttpResponse(obj.file, content_type='
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,
application/vnd.ms-excel')
return response
CodePudding user response:
file = model.objects.first() # here you are returning the entire object
instead of a file so you'll obviously get the object name instead of a xl file. Mos importantly is the xl file inside your object
