I am using Django with React. And I want to serve react public files from Django.
but django static files are served with a prefix url, like this
/static/favicon.ico
/static/manifest.json
but I want to serve files like this, without any prefix.
/favicon.ico
/manifest.json
/serviceworker.js
So how do I do it? please help
media
folder) with the same name of an existing URL path? You can make exceptions for files like favicon.ico
and manifest.json
in your web server (nginx etc) configuration. urls.py
for those special cases, e.g. re_path(r'^manifest\.json$', RedirectView.as_view(url='/static/manifest.json', permanent=True))
. I searched a lot about this and eventually found this as the best and efficient way
app/urls.py
import os
from django.urls import path
from django.conf import settings
from public import views
urlpatterns = []
SERVE_DIR = os.path.join(str(settings.BASE_DIR), "folder_path")
if os.path.exists(SERVE_DIR):
files = os.listdir(SERVE_DIR)
for f in files:
urlpatterns += [path(f, views.public)]
app/views.py
import os
from django.conf import settings
from django.views.static import serve
def public(request, public_url):
public_folder = os.path.join(str(settings.BASE_DIR), "folder_path")
return serve(request, public_url, document_root=public_folder)
Refer to Static Files
Changing STATIC_URL from '/static/' to '/' should work.