我已将我的Django 1.8项目移至我的生产服务器。它是功能性的,但它不像我的本地服务器那样加载我的静态文件(css,js或images)。我有两个必须具有相同代码库的存储库:我的本地机器和我的生产服务器。在我的本地计算机上找到并正确显示我的静态文件。在服务器上他们不是。
以下是生产目录树:
tmws
- tmws
- static
- tmws
- css
screens.css
- images
xxxxxx.png
main_style.css
这是我的settings.py for production
import os
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS
MASTER_BASE_DIR = os.path.dirname(__file__)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'something'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'polls',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'project',
'django_tables2'
)
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',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'tmws.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(MASTER_BASE_DIR, 'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.core.context_processors.request'
],
},
},
]
WSGI_APPLICATION = 'tmws.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(MASTER_BASE_DIR, "static"),
#'/var/www/static/',
)
这是我的base.html for production
{% load staticfiles %}
<html>
<head>
<meta charset="UTF-8">
<title>Tyler Millwork and Supply</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link rel="stylesheet" type="text/css" href="{% static 'tmws/main_style.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'tmws/css/screen.css' %}" />
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}django_tables2/themes/paleblue/css/screen.css" />
</head>
<body>
....
这是我的apache2.conf文件,用于生产
...
WSGIPythonPath /var/www/tmws
...
这是我的site.conf文件,用于生产
...
ServerAdmin ahardin@hardinresources.com
ServerName tmws.hardinresources.com
DocumentRoot /var/www/tmws
WSGIScriptAlias / /var/www/tmws/tmws/wsgi.py
<Directory /var/www/tmws/tmws>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
...
这是我的生产现场
以下是我在生产服务器上收到的错误
http://tmws.hardinresources.com/static/tmws/main_style.css Failed to load resource: the server responded with a status of 404 (NOT FOUND)
http://tmws.hardinresources.com/static/tmws/css/screen.css Failed to load resource: the server responded with a status of 404 (NOT FOUND)
http://tmws.hardinresources.com/django_tables2/themes/paleblue/css/screen.css Failed to load resource: the server responded with a status of 404 (NOT FOUND)
http://tmws.hardinresources.com/static/tmws/images/TMWS_Logo.png Failed to load resource: the server responded with a status of 404 (NOT FOUND)
http://tmws.hardinresources.com/favicon.ico Failed to load resource: the server responded with a status of 404 (NOT FOUND)
http://tmws.hardinresources.com/static/tmws/css/screen.css Failed to load resource: the server responded with a status of 404 (NOT FOUND)
http://tmws.hardinresources.com/django_tables2/themes/paleblue/css/screen.css Failed to load resource: the server responded with a status of 404 (NOT FOUND)
http://tmws.hardinresources.com/static/tmws/main_style.css Failed to load resource: the server responded with a status of 404 (NOT FOUND)
以下是我在本地服务器上收到的runserver消息
[23/May/2016 16:53:53] "GET / HTTP/1.1" 200 4061
[23/May/2016 16:53:53] "GET /django_tables2/themes/paleblue/css/screen.css HTTP/
1.1" 404 2354
[23/May/2016 16:53:56] "GET /static/tmws/main_style.css HTTP/1.1" 200 445
[23/May/2016 16:53:56] "GET /static/tmws/css/screen.css HTTP/1.1" 200 2774
[23/May/2016 16:53:56] "GET /static/tmws/img/header-bg.png HTTP/1.1" 404 1679
[23/May/2016 16:53:56] "GET /static/tmws/img/arrow-inactive-up.png HTTP/1.1" 404
1703
[23/May/2016 16:53:56] "GET /static/tmws/img/pagination-bg.gif HTTP/1.1" 404 169
1
[23/May/2016 16:53:56] "GET /static/tmws/images/Seamless-Wood-1.jpg HTTP/1.1" 20
0 21678
[23/May/2016 16:53:56] "GET /static/tmws/images/TMWS_Logo.png HTTP/1.1" 200 1610
51
[23/May/2016 16:53:56] "GET /favicon.ico HTTP/1.1" 404 2252
[23/May/2016 16:54:03] "GET /django_tables2/themes/paleblue/css/screen.css HTTP/
1.1" 404 2354
如何让我的生产服务器加载静态文件?
感谢您的帮助!
答案 0 :(得分:1)
尝试在Apache config
中为静态目录添加别名static void Main()
{
bool Example = false;
Console.Write((if(!Example){"Example is false"}else{"Example is true"}));
}
//Desired outcome of when the code shown above is
//executed would be for the console to output:
//Example is false
目录条目:
Alias /static/ /var/www/tmws/twms/static/
...假设您的静态目录是/ var / www / tmws / twms / static /,即。
我现在会在Django设置文件中更改你的SECRET_KEY,因为你已经在上面的问题中粘贴了它。
供参考:https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/modwsgi/#serving-files