0

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.

2

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @John Bull. However, It's not csv file I want to serve. I want django equivalent of res.sendFile() found in expressjs
Try: the_file = open("somefile.txt", "wb", buffering=False) response = json(the_file)
0

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')) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.