django Whitenoise在本地提供静态文件

fykwrbwg  于 2023-08-08  发布在  Go
关注(0)|答案(2)|浏览(148)

正如他们的文档here中所描述的,我将我的应用程序配置为本地提供静态文件。
我面临的唯一问题是,我无法确定是django还是whitenoise在服务静态文件?
我遵循的步骤:

pip install whitenoise # install whitenoise
pip install brotlipy # install brotlipy for compression

INSTALLED_APPS = [

# default django apps
'django.contrib.messages',

# REMOVE IN PRODUCTION
# See: http://whitenoise.evans.io/en/stable/django.html#using-whitenoise-in-development
'whitenoise.runserver_nostatic',

'django.contrib.staticfiles',
# other apps
]

# add white-noise middleware

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',

    # static files serving
    'whitenoise.middleware.WhiteNoiseMiddleware',

    'django.contrib.sessions.middleware.SessionMiddleware',
     # other middlewares
]

# add staticfiles 
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

# run collecstatic
python manage.py collectstatic

# restart the server
python manage.py runserver

# This gives me following

[10/Apr/2018 12:12:40] "GET /static/debug_toolbar/css/print.css HTTP/1.1" 304 0
[10/Apr/2018 12:12:40] "GET /static/chartkick.js HTTP/1.1" 304 0
[10/Apr/2018 12:12:40] "GET /static/debug_toolbar/js/jquery_pre.js HTTP/1.1" 304 0

However, I expect something like this,

[10/Apr/2018 12:12:40] "GET /static/debug_toolbar/css/print.636363s6s.css HTTP/1.1" 304 0
[10/Apr/2018 12:12:40] "GET /static/chartkick.2623678s3cdce3.js HTTP/1.1" 304 0
[10/Apr/2018 12:12:40] "GET /static/debug_toolbar/js/jquery_pre.6276gdg3js8j.js HTTP/1.1" 304 0

字符串
如何检查whitenoise是否正常工作,以及它是否为静态文件提供服务?

bkhjykvo

bkhjykvo1#

既然你已经有一段时间没有问这个问题了,我想你现在已经找到了答案。但是如果其他人也遇到了这个问题,我发现如果你有DEBUG=False,Django就不会提供静态文件。所以如果你有这样的设置,你仍然看到静态,它很可能是由白噪声。

uqdfh47h

uqdfh47h2#

这是一个古老的问题,但我在这里回答子孙后代。当运行python manage.py runserver时,runserver接管静态文件的管理。但是,您可以运行python manage.py runserver --nostatic或在www.example.com的INSTALLED_APPS中添加“whiternoise.runserver_nostatic”作为第一个setting.py,请参见此处

相关问题