Search code examples
djangoapachehttpdjango-channelsdaphne

How to serve static media with Daphne 2.0 on django


I'm new on daphne and I would like to know how to deploy a django app running on daphne, on ubuntu server. I already configured the app like the documentation said, and works fine, except that the static-files (js,css,imgs,etc) doesn't load. What I need to do?


Solution

  • Sorry, was a mistake. I recently noticed that when I was using Channels 1.8, I had this code on routing.py on production

    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,
    }
    

    Its likely that was the reason. that on 1.8 was working and 2.0 don't.

    Besides

    Andrew Godwin (Mantainer of daphne and channels) commented me

    "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"