Search code examples
djangodjango-templatesdjango-staticfilesdjango-statistics

Django hosting static files on the server


I am trying to deploy my Django project on the server. But, when I use it, the static file on Django can not be read correctly

I deploy my project on Debian server. The static file of course in same server, I have succeeded in deploying my project. But static files like css still can not appear in my project

This is my settings files:

"""
Django settings for akun project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

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


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '8g*v#sf1i0y#+@5jyy$kk)wlixu*9yo(t$&1n%59ip*391sy@u'

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'simofa',
    'accounts',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'akun.urls'

WSGI_APPLICATION = 'akun.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'pgina',
        'USER': 'root',
        'PASSWORD': '123',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Jakarta'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'

# template location
TEMPLATE_DIRS = (
        os.path.join(os.path.dirname(PROJECT_ROOT), "static", "templates"),
        '/home/boss/kantor/akun/templates/', 
    )

if DEBUG:
    MEDIA_URL = '/media/'
    STATIC_ROOT = os.path.join(os.path.dirname(PROJECT_DIR),"static","static-only")
    MEDIA_ROOT = os.path.join(os.path.dirname(PROJECT_DIR),"static","media")
    STATICFILES_DIRS = os.path.join(os.path.dirname(PROJECT_DIR),"static","static"),

i'm trying to change STATIC_URL='/static/' to the url STATIC_URL='http://www.url.com/my_project/static' but the result still doesn't appears

When i try in my localhost, it works properly.

how is the solution ?


Solution

  • In production you need first to define STATIC_ROOT and then run collectstatic in order to have your static files collected there.

    After running collectstatic you should be able to cd to the dir associated to STATIC_ROOT and see the files.

    EDIT: the code below should be added in he Apache conf file, not in the Django settings

    Finally if you are using Apache (and you are serving the files from the same server where you are running the Django app) you will need to serve the path of the STATIC_ROOT under the url defined in STATIC_URL, for example assuming STATIC_URL is /static/:

    Alias /static/ /path/to/mysite.com/static_root_directory/
    

    and then set permissions:

    <Directory /path/to/mysite.com/static_root_directory>
    Require all granted
    </Directory>
    

    PS you didn't provide many details about your environment (server, static on same server or not) so I had to make assumptions. If you provide more details I'm happy to help.