Why does DEBUG=False setting make my django Static Files Access fail?

后端 未结 14 1908
忘了有多久
忘了有多久 2020-11-22 05:16

Am building an app using Django as my workhorse. All has been well so far - specified db settings, configured static directories, urls, views etc. But trouble started sneaki

相关标签:
14条回答
  • 2020-11-22 05:56

    You can debug this in many different ways. Here's my approach.

    localsettings.py:

    DEBUG = False
    DEBUG404 = True
    

    urls.py:

    from django.conf import settings
    import os
    
    if settings.DEBUG404:
        urlpatterns += patterns('',
            (r'^static/(?P<path>.*)$', 'django.views.static.serve',
             {'document_root': os.path.join(os.path.dirname(__file__), 'static')} ),
        )
    

    Be sure to read the docs ;)

    https://docs.djangoproject.com/en/2.0/howto/static-files/#limiting-use-to-debug-true

    0 讨论(0)
  • 2020-11-22 05:56

    Although it's not safest, but you can change in the source code. navigate to Python/2.7/site-packages/django/conf/urls/static.py

    Then edit like following:

    if settings.DEBUG or (prefix and '://' in prefix):
    

    So then if settings.debug==False it won't effect on the code, also after running try python manage.py runserver --runserver to run static files.

    NOTE: Information should only be used for testing only

    0 讨论(0)
  • 2020-11-22 05:57

    If you still need to server static locally (e.g. for testing without debug) you can run devserver in insecure mode:

    manage.py runserver --insecure
    
    0 讨论(0)
  • 2020-11-22 06:00

    Johnny's answer is great, but still didn't work for me just by adding those lines described there. Based on that answer, the steps that actually worked for me where:

    1. Install WhiteNoise as described:

      pip install WhiteNoise
      
    2. Create the STATIC_ROOT variable and add WhiteNoise to your MIDDLEWARE variable in settings.py:

      #settings.py
      MIDDLEWARE = [
          'django.middleware.security.SecurityMiddleware',
          'whitenoise.middleware.WhiteNoiseMiddleware', #add whitenoise
          'django.contrib.sessions.middleware.SessionMiddleware',
          ...
      ]
      
      #...
      
      STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') ##specify static root
      
    3. Then, modify your wsgi.py file as explained in Johnny's answer:

      #wsgi.py
      from django.core.wsgi import get_wsgi_application
      from whitenoise.django import DjangoWhiteNoise
      
      application = get_wsgi_application()
      application = DjangoWhiteNoise(application)
      
    4. After that, deploy your changes to your server (with git or whatever you use).

    5. Finally, run the collectstatic option from your manage.py on your server. This will copy all files from your static folders into the STATIC_ROOT directory we specified before:

      $ python manage.py collectstatic
      

      You will now see a new folder named staticfiles that contains such elements.

    After following these steps you can now run your server and will be able to see your static files while in Production mode.

    Update: In case you had version < 4 the changelog indicates that it's no longer necessary to declare the WSGI_APPLICATION = 'projectName.wsgi.application' on your settings.py file.

    0 讨论(0)
  • 2020-11-22 06:02

    I agree with Marek Sapkota answer; But you can still use django URFConf to reallocate the url, if static file is requested.

    Step 1: Define a STATIC_ROOT path in settings.py

    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    

    Step 2: Then collect the static files

    $ python manage.py collectstatic
    

    Step 3: Now define your URLConf that if static is in the beginning of url, access files from the static folder staticfiles. NOTE: This is your project's urls.py file:

    from django.urls import re_path
    from django.views.static import serve
    
    urlpattern += [
      re_path(r'^static/(?:.*)$', serve, {'document_root': settings.STATIC_ROOT, })
    ]
    
    0 讨论(0)
  • 2020-11-22 06:04

    This is Exactly you must type on terminal to run your project without DEBUG = TRUE and then you see all assets (static) file is loading correctly On local server .

    python manage.py runserver --insecure 
    

    --insecure : it means you can run server without security mode

    0 讨论(0)
提交回复
热议问题