1

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 ?

ranggatry
  • 125
  • 1
  • 15

2 Answers2

2

Static files dirs should be a tuple

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
Mikeec3
  • 649
  • 3
  • 9
2

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.

mastazi
  • 1,623
  • 26
  • 41
  • Thank you @mastazi . I will give more details of my environment so i will update my question. – ranggatry Apr 29 '15 at 06:24
  • Ps edit your question in order to hide the secret key which is in your settings.py. Also remember that you have to edit allowed hosts in order to include your domain, or you won't be able to set debugging to false. – mastazi Apr 29 '15 at 11:32
  • thank you very much. it works. i must set `STATIC_URL = '/profil/static/'` and in my html template i must add `/profil` in front of my previous `/url/` – ranggatry Apr 30 '15 at 03:07
  • I'm glad that it works. If you add the configurations for Apache that I mentioned (the `Alias` and the `` parts) in your Apache configuration file, then you will not need to add `/profil` in your templates. – mastazi Apr 30 '15 at 04:41
  • I have edited my answer to make more clear which parts of my answer refer to Apache. The advantage of not having `/profil` in your templates is that you can sync your app between localhost and production server without manually modifying anything. – mastazi Apr 30 '15 at 04:50