如何在 Django 上使用 Daphne 2.0 服务静态媒体

How to serve static media with Daphne 2.0 on django

我是 daphne 的新手,我想知道如何在 ubuntu 服务器上的 daphne 上部署 django 应用程序 运行。我已经按照文档所述配置了该应用程序,并且工作正常,只是静态文件(js、css、imgs 等)未加载。我需要做什么?

使用这些设置,它们对我来说效果很好。我们这里有两个单独的文件夹。一个用于媒体文件,另一个用于静态文件。

STATIC_URL = '/static/'

STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static_my_proj"),
]

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", 
"static_root")

MEDIA_URL = '/media/'
MEDIA_ROOT = 
os.path.join(os.path.dirname(BASE_DIR),"static_cdn","media_root")

抱歉,弄错了。我最近注意到当我使用 Channels 1.8 时,我在 routing.py 上有这段代码 制作中

from channels.staticfiles import StaticFilesConsumer
from . import consumers

channel_routing = {
    # This makes Django serve *emphasized text*static files from settings.STATIC_URL, similar
    # to django.views.static.serve. This isn't ideal (not exactly production
    # quality) but it works for a minimal example.
    'http.request': StaticFilesConsumer(),
    # Wire up websocket channels to our consumers:
   'websocket.connect': consumers.ws_connect,
   'websocket.receive': consumers.ws_receive,
   'websocket.disconnect': consumers.ws_disconnect,
}

可能是这个原因。在 1.8 上工作,而 2.0 上没有。

此外

Andrew Godwin(达芙妮和频道的维护者)评论了我

"Daphne will only serve static files from the same process in local dev via runserver - once you deploy to production, you need to run collectstatic and serve static files separately as you can read here: https://docs.djangoproject.com/en/2.0/howto/static-files/deployment/#serving-static-files-in-production"