Lead Profile All Articles
I am an ardent software developer and eager lifelong learner of code. My passion is the python language with particular emphasis in the 'django' and 'opencv' packages. I have created this blog to share my e... Read More
Feb. 14, 2019 Chris Shaw 1312 views
Getting the static files to display correctly when hosting your django site on a shared server is quick and easy in a few steps.
After getting your django project started on a shared server, things don't initially look quite right when we try and open the admin area. The reason for this is that the css and js files are not being served. For reference, the official docs can be read here.
Open for editing mysite/settings.py and right at the bottom (normally) find:
STATIC_URL = '/static/'
Add the following:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Open for editing mysite/urls.py and add the following:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
To collect the static files ready to server, shell into the server and activate the virtual environment, then run:
(domain_html:3.5)username@server[~/domain_html]# python manage.py collectstatic
119 static files copied to '/home/username/domain_html/static'.
Finally, restart the app from your cPannel. When we have a look at the admin login, it will now display correctly.
Done, now we are looking good.