Trick to use Django to serve React app

Django backend and React.js frontend

Instead of using template language in view part of MVC in frameworks, modern way is to separate apps into backend and frontend. The idea is using javascript fetch() or Axios.get()/ in frontend to call APIs in backend frameworks e.g. Django's Django Rest Framework which is the one I'm going to talk about or Node's Express.js framework which I will discuss in future.

How they serve using template

I found the common ways need to set up a few steps e.g. adding function base view to render frontend index.html in ` `,

The code in looks like

def index(request):
   return render(request, "build/index.html")


from django.urls import path
from demo.views import index
urlpatterns = [
    path("", index, name="index")

installing django-cors-headers package to fix CORS issue and then in ``, adding frontend directory path to TEMPLATES and frontend static folder in build to STATICFILES_DIRS .
   'corsheaders.middleware.CorsMiddleware',  # added to solve CORS
   'django.middleware.common.CommonMiddleware',  # added to solve CORS

       'BACKEND': 'django.template.backends.django.DjangoTemplates',
       'DIRS': [os.path.join(BASE_DIR, 'frontend')]  # added the root folder of frontend here
       'APP_DIRS': True,
       'OPTIONS': {
           'context_processors': [
STATIC_URL = '/static/'
   os.path.join(BASE_DIR, 'frontend', "build", "static"),  # update the STATICFILES_DIRS

I feel this is too much trouble. And the static content might not be rendering correctly e.g. images might not display due to static path issue.

How I serve using serve function

I figured out an easier way to do it. The trick is create a my_serve function in . I refer to the serve function in django.conf.urls.static and rewrite it to make it work as using it directly doesn't work as expected.


from django.conf import settings
from django.conf.urls.static import static, serve
from django.contrib import admin
from django.urls import path, re_path, include
from rest_framework.routers import DefaultRouter
from django.http import (
    FileResponse, Http404, HttpResponse, HttpResponseNotModified,
import posixpath
from pathlib import Path
from django.utils._os import safe_join
import mimetypes
from django.utils.http import http_date
from django.utils.translation import gettext as _
import demo.views
def my_serve(request, path, document_root=None, show_indexes=False):
    path = posixpath.normpath(path).lstrip('/')
    fullpath = Path(safe_join(document_root, path))
    if fullpath.is_dir():
        fullpath = Path(safe_join(document_root, 'index.html'))
    if not fullpath.exists():
        raise Http404(_('"%(path)s" does not exist') % {'path': fullpath})
    # Respect the If-Modified-Since header.
    statobj = fullpath.stat()
    content_type, encoding = mimetypes.guess_type(str(fullpath))
    content_type = content_type or 'application/octet-stream'
    response = FileResponse('rb'), content_type=content_type)
    response["Last-Modified"] = http_date(statobj.st_mtime)
    if encoding:
        response["Content-Encoding"] = encoding
    return response

router = DefaultRouter()
router.register(r'testApi', demo.views.TestViewSet)

urlpatterns = [
    re_path(r'^api/v1/', include(router.urls)),
    re_path(r'^(?P<path>.*)$', my_serve, { 'document_root': settings.FRONTEND_ROOT }),


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

FRONTEND_ROOT = os.path.abspath(os.path.join(BASE_DIR, '..', 'frontend', 'build'))

Doing it this way, I only need to change one place i.e. adding FRONTEND_ROOT in Then Django hands over all the routing control to react router.

Trick to use Django to serve React app


Compare Disqus vs Valine comment system and how to set up Valine


Depth First Search in Practice