1

我正在尝试在服务器上部署我的 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/'为 urlSTATIC_URL='http://www.url.com/my_project/static' 但结果仍然没有出现

当我在我的本地主机中尝试时,它可以正常工作。

解决方案如何?

4

2 回答 2

2

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

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
于 2015-04-29T03:47:42.230 回答
2

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

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

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

最后,如果您使用的是 Apache(并且您正在从运行 Django 应用程序的同一台服务器上提供文件),您将需要提供中STATIC_ROOT定义的 url 下的路径STATIC_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您没有提供有关您的环境的许多详细信息(服务器,同一服务器上的静态与否),因此我不得不做出假设。如果您提供更多详细信息,我很乐意提供帮助。

于 2015-04-29T03:55:23.380 回答