我正在尝试在服务器上部署我的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/'
更改为网址STATIC_URL='http://www.url.com/my_project/static'
但结果仍然没有出现
当我在本地主机中试用时,它可以正常工作。
解决方案如何?
答案 0 :(得分:2)
静态文件目录应该是一个元组
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
答案 1 :(得分:2)
在制作中,首先需要定义STATIC_ROOT
,然后运行collectstatic
以便在那里收集静态文件。
运行collectstatic
后,您应该能够cd到与STATIC_ROOT
相关联的目录并查看文件。
编辑:下面的代码应该添加到Apache conf文件中,而不是添加到Django设置中
最后,如果您正在使用Apache(并且您正在运行Django应用程序所在的同一服务器上提供文件),则需要在{{1}中定义的URL下提供STATIC_ROOT
的路径。例如,假设STATIC_URL
是/ static /:
STATIC_URL
然后设置权限:
Alias /static/ /path/to/mysite.com/static_root_directory/
PS你没有提供很多关于你的环境的细节(服务器,静态在同一台服务器上)所以我不得不做出假设。如果您提供更多详细信息,我很乐意为您提供帮助。