0

I am trying to hook up Django and React. See here or here. This is serving up some Django URLs, but then passing the rest on to React.

I want to serve up not only React's index.html, but also any of its files from the "public" directory. There is HTML, JSON, could be images or icon files, etc. A mix of text files and binary files.

Here is some code to serve up a binary file (like an image) with FileResponse:

from django.http import FileResponse


def view_func(request):
    # .. find a static file to serve
    content = open(some_file_path, 'rb')
    return FileResponse(content)

Note that it opens content as a binary file (rb). But if I am serving index.html, presumably that is not binary. For example, it might be UTF-8. (I think that's the default if I open with r, no binary.)

Is there an easy way to serve up a file in Django whether it is text or binary?

P.S. I feel like there should be a way to hook up Django's static file serving, but after a few hours of fiddling, it is harder than it looks. This is not going to get super-high traffic, so I'm fine with this.

dfrankow
  • 18,326
  • 38
  • 134
  • 193

0 Answers0