C-Bizz
C-Bizz

Reputation: 654

How to return a file in django

I have a django app through which I would like to serve an index.html file. This html file isn't in default django template, rather I have the index.html in a react build folder. I don't want to render the template, rather I want to send it. The render method isn't working, and I don't know if there is any django method that can serve the file. What I want to accomplish can be done using res.sendFile() in expressjs.

def home(request):
    return FileResponse(request, 'home/home.html')    

The FileResponse method isn't working here.

Upvotes: 0

Views: 2305

Answers (2)

F30
F30

Reputation: 1192

FileResponse has a single required argument, an open file-like object.

This means you should not provide request to it. On the other hand, you need to open() the file yourself (preferably in binary mode) instead of just providing its path.

def home(request):
    return FileResponse(open('home/home.html', 'rb'))

Upvotes: 0

John_B
John_B

Reputation: 51

Maybe you can try this, you can use it specifically for CSV, but if you want in Djagngo's guide you can see the example for PDF Link

Upvotes: 1

Related Questions