The problem of not finding React static files in Python
The back end of my project is Python and I have used RteactJS for the front end.
When using the python manage.py runserver command, the project runs successfully, but it does not find the static files.
setting:
import os
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
STATICFILES_DIRS = [
os.path.join(BASE_DIR, '/clientApp/build/static'),
]
STATIC_ROOT = BASE_DIR / "static"
STATIC_URL = '/static/'
url :
urlpatterns = [
path('', index, name='index'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Did you collect your statics files?
If not use this command:
python manage.py collectstatic
after that, you can use this command to run your server:
python manage.py runserver
If it's not working again try to change your code with this:
settings:
import os
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
STATICFILES_DIRS = [
os.path.join(BASE_DIR / "clientApp/build/static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static-cdn', 'static-root')
STATIC_URL = '/static/'
urls:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', index, name='index'),
]
if settings.DEBUG:
# add root static files
urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
for more information, you can read this document: https://docs.djangoproject.com/en/5.0/howto/static-files/