Django: can't load static files from template using Ubuntu 14.04/Django Droplet

May 20, 2015 4.6k views
Django Nginx Ubuntu

Hello everyone,
I'm trying to render a template page with my Django project running on a Ubuntu 14.04 Droplet, but I'm having a lot of issues loading static files from the template.

My project structure:


Here it is my nginx configuration file:

upstream app_server {
    server fail_timeout=0;

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name _;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/workspace/app-toscana/django/djangoapptoscana/djangoapptoscana/media;

    # your Django project's static files - amend as required
    location /static {
        #alias /home/workspace/app-toscana/django/djangoapptoscana/djangoapptoscana/static;
        alias /home/workspace/app-toscana/django/djangoapptoscana/static;

    # Proxy the static assests for the Django Admin panel
    location /static/admin {
       alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;

The settings file:


Django settings for djangoapptoscana project.

For more information on this file, see

For the full list of settings and their values, see

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Quick-start development settings - unsuitable for production
# See

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '0^rkr0-u%ic)yo^qc()vp8ii&mn&irk+ie_o=swp7qd82ervyh'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True



# Application definition



ROOT_URLCONF = 'djangoapptoscana.urls'

WSGI_APPLICATION = 'djangoapptoscana.wsgi.application'

# Database

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'USER': 'test',
        'PASSWORD': 'test',
        'HOST': 'localhost',
        'PORT': '',

# Internationalization



USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)


#STATIC_PATH = os.path.join(BASE_DIR, 'static')

STATIC_ROOT = '/home/workspace/app-toscana/django/djangoapptoscana/static/'

STATIC_URL = '/static/' # You may find this is already defined as such.


Template page:

{% load staticfiles %}
<!DOCTYPE html>
    <meta charset="UTF-8">
    <title>AdminLTE 2 | Dashboard</title>
    <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
    <!-- Bootstrap 3.3.4 -->
    <link href="{% static 'bootstrap/css/bootstrap.min.css' %}" rel="stylesheet" type="text/css" />
    <!-- Font Awesome Icons -->
    <link href="" rel="stylesheet" type="text/css" />
    <!-- Ionicons -->
    <link href="" rel="stylesheet" type="text/css" />
    <!-- jvectormap -->
    <link href="{% static 'plugins/jvectormap/jquery-jvectormap-1.2.2.css' %}" rel="stylesheet" type="text/css" />
    <!-- Theme style -->
    <link href="{% static 'dist/css/AdminLTE.css' %}" rel="stylesheet" type="text/css" />
    <!-- AdminLTE Skins. Choose a skin from the css/skins
         folder instead of downloading all of them to reduce the load. -->
    <link href="{% static 'dist/css/skins/_all-skins.min.css' %}" rel="stylesheet" type="text/css" />

    <link href="{% static 'dist/css/ol.css' %}" rel="stylesheet" type="text/css" />
    <script src="{% static 'dist/js/ol.js' %}"></script>

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src=""></script>
        <script src=""></script>
        <div>Hello world!</div>

When I try to get some static file, I receive a 404 error.
Any suggestion? I tried different configuration without any results.

1 comment
3 Answers

Hopefully, I found a solution to this problem. It was a permission issue. On my DigitalOcean machine I set the owner of the django app directory to root:root

chown -R root:root /home/django/djangoapptoscana/dashboard

and the owner of the static files directory to django:django

chown -R django:django /home/django/djangoapptoscana/djangoapptoscana/static

This was the solution to my problem too, Thanks for updating!

Have another answer? Share your knowledge.