2016-05-23 1 views
0

J'ai déplacé mon projet Django 1.8 vers mon serveur de production. C'est fonctionnel, mais il ne charge pas mes fichiers statiques (css, js ou images) comme mon serveur local. J'ai deux dépôts qui ont la même base de code: ma machine locale et mon serveur de production. Sur ma machine locale mes fichiers statiques sont trouvés et affichent correctement. Sur le serveur, ils ne le sont pas.Django Fichiers statiques de production non chargés

Voici une arborescence de répertoires pour la production:

tmws 
- tmws 
    - static 
    - tmws 
     - css 
     screens.css 
     - images 
     xxxxxx.png 
     main_style.css 

Voici ma settings.py pour la 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/', 
) 

Voici ma base.html pour la 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> 
.... 

ici est mon fichier apache2.conf pour la production

... 
WSGIPythonPath /var/www/tmws 
... 

Voici mon fichier site.conf pour la production

... 
     ServerAdmin [email protected] 
     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> 
... 

Voici mon site de production pour la production

my site

Voici l'erreur que je reçois sur mon serveur de production

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) 

Voici les messages runserver que je reçois sur mon serveur local

[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 

Comment obtenir mon serveur de production pour charger des fichiers statiques?

Nous vous remercions de toute l'aide!

Répondre

1

Essayez d'ajouter un alias dans votre répertoire statique dans la configuration Apache

Alias /static/ /var/www/tmws/twms/static/ 

Et une entrée du répertoire:

<Directory /var/www/tmws/twms/static> 
Require all granted 
</Directory> 

... en supposant que votre répertoire est statique/var/www/tmws/twms/static /, c'est-à-dire.

Et je voudrais changer votre SECRET_KEY dans le fichier de paramètres Django maintenant, puisque vous l'avez collé dans votre question ci-dessus.

Pour la référence: https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/modwsgi/#serving-files

+0

Merci !!!!!!!! – user908759

+0

Content de pouvoir aider! –