Reputation: 820
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
Upvotes: 0
Views: 393
Reputation: 820
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)
Upvotes: 1
Reputation: 26
Refer to Static Files
Changing STATIC_URL from '/static/' to '/' should work.
Upvotes: 0