I'm trying to deploy my Django instructor's (Udemy) 'real estate' project. Here is Brad Traversy’s gist.
On my VPS I'm running Ubuntu 18.04, Python 3.6.8 and Django 2.2.4.
I've followed Brad’s guide for deploying Django. I kinda got it to run but the static files (CSS, JS and images) are not being served. It's an issue with my configuration and it is not clear to me what I'm doing wrong here or what exactly I am missing.
Here is what the end result should look like:
Here is what mine actually looks like:
Exploring other questions and answers on SO, inside my settings.py, I've tried swapping in (and out) different paths into STATIC_ROOT, MEDIA_ROOT, STATICFILES_DIRS using the join method. I’ve also tried adding a number of different list items into the urlpatterns variable inside urls.py. After trying each new potential change, I enter python manage.py collectstatic
and then I run the server. Sometimes there would be a trace back preventing the server from running. In these situations, I just rolled back the change to a setting which was known to work or alternatively, I just tried something else entirely which would enable the server to run without a trace back.
As per @IrrupTor's answer, when running this project locally, the static and media files parse perfectly. So this confirms the issue exists in my remote configuration on my VPS. What other information could I provide to better clarify why static and media files are not loading on my VPS?
Here are some of the other questions on SO which I have used already:
- Django 2.2 Not Serving Static Files
- Static Files not being displayed in Production
- Django: Static Files not displayed on deployment server
Here is the output in my Django shell:
python manage.py runserver 0.0.0.0:8000
Performing system checks...
System check identified no issues (0 silenced).
September 14, 2019 - 00:44:11
Django version 2.2.4, using settings 'btre.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
[14/Sep/2019 00:44:15] "GET / HTTP/1.1" 200 13775
[14/Sep/2019 00:44:15] "GET /static/css/bootstrap.css HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/css/all.css HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/css/style.css HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/css/lightbox.min.css HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/js/jquery-3.3.1.min.js HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/img/logo.png HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/js/lightbox.min.js HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/js/bootstrap.bundle.min.js HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/js/main.js HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/js/lightbox.min.js HTTP/1.1" 404 77
[14/Sep/2019 00:44:15] "GET /static/js/main.js HTTP/1.1" 404 77
Here is my urls.py:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', include('pages.urls')),
path('listings/', include('listings.urls')),
path('accounts/', include('accounts.urls')),
path('contacts/', include('contacts.urls')),
path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Here are the relevant lines in my settings.py:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'btre/static')
]
# Media Folder Settings
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
Here is my file tree:
Here is my (forked) source code repo on GitHub.
One final note: Right now I am using Django's test server on my VPS. I realize this is not best practices because it is not secure. I am merely deploying with the Django's built in server as a stepping stone. Once the static and media files load I'll promptly switch to nginx and gunicorn, as described in the original guide.