Django 在服务器上托管静态文件

Django hosting static files on the server

我正在尝试在服务器上部署我的 Django 项目。但是,当我使用它时,无法正确读取Django 上的静态文件

我在 Debian 服务器上部署我的项目。静态文件当然在同一台服务器上,我已经成功部署了我的项目。但是像css这样的静态文件还是不能出现在我的项目中

这是我的设置文件:

"""
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"),

我正在尝试将 STATIC_URL='/static/' 更改为 url STATIC_URL='http://www.url.com/my_project/static' 结果还是没有出现

当我在本地主机上尝试时,它工作正常。

怎么解决的?

静态文件目录应该是一个元组

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

在生产环境中,您需要先定义 STATIC_ROOT,然后定义 运行 collectstatic,以便在那里收集您的静态文件。

在 运行ning collectstatic 之后,您应该能够 cd 到与 STATIC_ROOT 关联的目录并查看文件。

编辑:下面的代码应该添加到 Apache conf 文件中,而不是 Django 设置中

最后,如果您使用的是 Apache(并且您从与 运行Django 应用程序所在的同一台服务器提供文件),您将需要提供 STATIC_ROOT 下的路径STATIC_URL 中定义的 url,例如假设 STATIC_URL 是 /static/:

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

然后设置权限:

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

PS 你没有提供很多关于你的环境的细节(服务器,是否在同一台服务器上是静态的)所以我不得不做出假设。如果您提供更多详细信息,我很乐意提供帮助。