1

I have a problem with static files in my Django application.

I create a simple blog app with Django REST Framework and Angular 6. It's working fine in my local development environment, but I have a problem with deploying it to production. The thing is, the app is loading (I know that because the root route is a redirection to /app and I am being redirected correctly) but there are no static files loaded.

Here is part of my configuration related to the static files:

STATIC_URL = '/static/'
BASE_DIR = os.path.join(
    os.path.dirname(os.path.dirname(__file__)), '..', '..', '..'
)
STATIC_ROOT = os.path.join(BASE_DIR, 'app', 'static')
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'frontend'),
]

INSTALLED_APPS = [
    ...
    'django.contrib.staticfiles'
    ...
]

The paths are correct, I triple-checked them to be sure. And here are routes configured in Django application:

urlpatterns = [
    url('^api/', include(api_patterns)),
    url('^admin/', admin.site.urls),
    url('^app/', serve, kwargs={'path': 'index.html'}),
    url('^$', HomepageView.as_view(), name='homepage-redirection'),
]

I am able to run manage.py collectstatic without any errors and all static files are correctly copied to the static directory. Static files for admin application are not loaded correctly as well. Here is a rough structure of directories in my project:

├── app
│   └── (python code here)
├── frontend
│   └── (angular code here)
├── media
│   └── (empty for now)
└── static
    └── (static files for Django)

The application server is nginx with Phusion Passenger (this was configured by my the company I rent the server from). I don't really know what else I can add here. Does anyone have any idea what can be wrong here? Maybe some hint would be the fact that the API endpoints are not accessible. I have access to admin application, but not to API (both configured in the same urls file).

Mateusz Cisek
  • 775
  • 11
  • 22
  • 2
    Possible duplicate of [Why does DEBUG=False setting make my django Static Files Access fail?](https://stackoverflow.com/questions/5836674/why-does-debug-false-setting-make-my-django-static-files-access-fail) – Willem Van Onsem Jul 28 '18 at 20:24
  • I've seen such info before, but the thing is I am pretty sure that the server is configure correctly. I will check it again though. – Mateusz Cisek Jul 28 '18 at 21:05
  • So your nginx is correctly configured to serve files from the folder the static files are located at? Open your browser console and look at the 404 errors. Is the path what you expect it to be? – trixn Jul 28 '18 at 23:53
  • Can I see your nginx configuration please – Ali Jul 29 '18 at 03:57
  • I'm also going to guess this is an nginx problem. Are you sure that your app is explicitly serving the images from /static? If your angular app also has a /static being served there may be a conflict. Can you please post your server config? – wdfc Jul 29 '18 at 18:12
  • I wrote to my hosting supplier and he solved the problem. From the conversation, I understood that is was a problem of nginx configuration after all. – Mateusz Cisek Aug 09 '18 at 09:48

0 Answers0