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.

C-Bizz
  • 624
  • 9
  • 25

2 Answers2

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

John_B
  • 41
  • 8
  • Thanks @John Bull. However, It's not csv file I want to serve. I want django equivalent of `res.sendFile()` found in expressjs – C-Bizz Jun 29 '21 at 13:58
  • Try: the_file = open("somefile.txt", "wb", buffering=False) response = json(the_file) – John_B Jun 29 '21 at 14:25
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'))
F30
  • 1,036
  • 1
  • 10
  • 21