Search code examples
reactjsdjangodjango-react

Integrating react with django


I am learning how to integrate react with Django.. I found the tutorial by Dennis Ivy useful.. (linked below).

So according to that video, where we create a Django project, and create a separate folder with npx create-react-app appname, we need to npm run build every time we make a change to the react.. Is there a better way to do it?

my settings.py:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'reactapp/build'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]



STATIC_URL = '/static/'
STATICFILES_DIRS = [
    BASE_DIR / 'reactapp/build/static'
]

https://www.youtube.com/watch?v=F9o4GSkSo40&list=PLw4CVL8B1qWH_FsGE53Do_dSN-UdgGiGD&index=21


Solution

  • Since create-react-app is used, npm run start is a command you can use to host the react part of the application. This will start a server and host your application on localhost:3000.

    In general you can check the scripts section of the package.json file for javascript packages, most application have their commands in this section, to run them on the cli and to run them you'd need to run npm run <script name>