Static files not loaded error 404 with debug False

hi
i need help please !

while deploying django app in production with debug =False, i get 404 for all files in static folder

i tested all solutions that i found in forum, internet and youtube :frowning:

STATIC_URL = '/public/static/'
STATIC_ROOT = BASE_DIR / 'public/static'

and also i generate collectstatic

STATICFILES_DIRS = [
   os.path.join(BASE_DIR, "public/static"),
]

Django version 4.1
default Admin files there no custom

thank’s for help

What are you using as your web server in production? How is it configured for your static files?

For example when i runserver localy with “python manage.py runserver”
i get this

System check identified no issues (0 silenced).
May 22, 2023 - 17:20:34
Django version 4.1, using settings 'api_data.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[22/May/2023 17:20:37] "GET / HTTP/1.1" 200 1351
[22/May/2023 17:20:37] "GET /public/static/admin/css/responsive.css HTTP/1.1" 404 179
[22/May/2023 17:20:37] "GET /public/static/admin/css/base.css HTTP/1.1" 404 179
[22/May/2023 17:20:37] "GET /public/static/admin/css/dark_mode.css HTTP/1.1" 404 179
urlpatterns = [
#    path('graphql',GraphQLView.as_view(graphiql=True, schema=schema)),
    path("admin/", admin.site.urls),
    path("accounts/", include("portal.urls")),  
    path("accounts/", include("django.contrib.auth.urls")),
    path('', TemplateView.as_view(template_name='home.html'), name='home'), 
    
]

with Python 3.9.0

Do not use runserver as your production web server. Trust what the documentation says about this and create a proper deployment.

DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests.

[Emphasis added]

If you wish to test your server with debug=False, see the docs at How to manage static files (e.g. images, JavaScript, CSS) | Django documentation | Django

Also see How to deploy Django | Django documentation | Django

1 Like

locall i’m using windows
but in production ENV i’m using Azure WebApp

Azure web app documentation

in Django documentation it’s says that i will need WSGI server (Gunicorn) and a web server (Nginx).

but in my organization i should use Managed Services on Azure.

Wow, I can’t believe that the Microsoft docs actually recommend using runserver for Django.

At a minimum, I would look to modify whatever they’re doing to use gunicorn instead.

If you’re really insistent on using runserver, see the previous link on static files to see how you can configure your app to allow static files to be served by runserver.

2 Likes

when i test runserver i get this

Performing system checks...

System check identified no issues (0 silenced).
May 23, 2023 - 14:29:39
Django version 4.1, using settings 'api_data.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Error: That port is already in use.

even if i specify my production url

ALLOWED_HOSTS = ['prod.azurewebsites.net']

and also when i try to serve static files i get 404 for every file (with disabling cache on my browser)

That is being caused by you having something else running on your system using port 8000. You either need to find what process is using that port and stop it, or picking a different port to use.

Hi @KenWhitesell
Halliluja !

i fixed my problem, thanks to