0

I have a Django project that includes two React apps, and I am using Django to serve them. I have set up my settings.py file to include the directories for both React apps in the TEMPLATES and STATICFILES_DIRS sections as follows:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'clients/build/'),
            os.path.join(BASE_DIR, 'sephora-app/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',
            ],
        },
    },
]

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'clients/build/static'),
    os.path.join(BASE_DIR, 'sephora-app/build/static'),
]

urls.py

from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    path('a/', views.index, name='index'),
    path('b/', views.index1, name='index1'),
]

views.py

from django.shortcuts import render

def index(request):
    return render(request, 'index.html')

def index1(request):
    return render(request, 'sephora-app/build/index.html')

When I try to access the React app at http://localhost:8000/b/, Django gives me the following error message:

TemplateDoesNotExist at /b/
sephora-app/build/index.html

It looks like Django is not finding the index.html file in the correct directory even though it is specified in settings.py. I have tried changing the order of the directories in settings.py, but it only finds the first directory specified in DIRS. I have also tried setting DIRS to an empty list [], but that did not work either.

Can someone please help me figure out what is wrong with my configuration and how to make Django find the correct directory for the React app?

  • Does requesting http://localhost:8000/a/ work? It looks like since you specified a template folder as `/sephora-app/build/` and then try to get `sephora-app/build/index.html` django tries to find a file at `/sephora-app/build/sephora-app/build/index.html`. Maybe try setting second template folder as plain BASE_DIR? (also in every project I saw specified template folder without ending with `/` like `'DIRS': [os.path.join(BASE_DIR, 'templates')]` but I can't be sure if this might be dangerous is some way) – COSHW Mar 29 '23 at 23:38

0 Answers0