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?
Asked
Active
Viewed 5,216 times
4
-
I don't know how to is in Daphne but when You are using only Django the first thing is to set Static url in settings. After in every static href You must to add the command {% static 'app/pathtofile.jpg' %}. The last thing is to add {% load static %} in every html file where You want to serve statics. – killerbees Feb 09 '18 at 20:17
2 Answers
2
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"

Darwin
- 328
- 3
- 13
-1
Use these settings, they have worked fine for me. We have two separate folders here. One for media files and other for static files.
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")

Ishwar Jangid
- 279
- 2
- 17