问题
EDITED TO SHOW UPDATED CONFIGURATION
No static files are being shown in Production. Files are showing correctly in Development
settings.py
BASE_DIR = os.path.dirname(__file__)
STATIC_ROOT = '/opt/soundshelter/static/'
print "Base Dir: " + BASE_DIR #this returns /opt/soundshelter/soundshelter/soundshelter
print "Static Root: " + STATIC_ROOT #this returns /opt/soundshelter/static/
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
) #/opt/soundshelter/soundshelter/soundshelter/static
Files are being called in the applications with
<link href="{% static "css/portfolio-item.css" %}" rel="stylesheet">
Using Nginx and Gunicorn.
Nginx config:
server {
server_name 46.101.56.50;
access_log yes;
location /static {
autoindex on;
alias /opt/soundshelter/static/;
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
# error_log /opt/soundshelter/soundshelter/soundshelter/nginx-error.log;
}
Running python manage.py collectstatic
reports files were successfully copied but are still not being displayed.
Deployment handled by Fabric
from __future__ import with_statement
from fabric.api import *
from fabric.contrib.console import confirm
env.hosts = []
print "Here we go..."
def commit():
local("git add . && git commit")
def push():
local("git push origin master")
def prepare_deploy():
commit()
push()
def deploy():
code_dir = '/opt/soundshelter/soundshelter/'
with cd(code_dir):
run("git pull")
run("python manage.py collectstatic -v0 --noinput")
run("service nginx restart")
run("ps aux |grep gunicorn |xargs kill -HUP")
run("gunicorn -b PROD_IP soundshelter.wsgi:application")
commit()
push()
deploy()
回答1:
First of all, it seems to me that your edited STATICFILES_DIRS
points to the wrong folder, since you have /static/
folder, not /staticfiles/
. Should be as it was originally:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Second, STATIC_ROOT
should point to the static folder which will be served by webserver in pro (but preferably not in project folder). In your case:
STATIC_ROOT="/opt/soundshelter/soundshelter/soundshelter/static/"
I usually place static folder near the project one and use dynamic STATIC_ROOT
instead of hardcoding:
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
#this will alow to collect your static files in /opt/soundshelter/soundshelter/staticfiles/static
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'staticfiles/static')
Now you should do collectstatic
and it will collect all static files in the STATIC_ROOT
directory.
回答2:
BAD SOLUTION - DO NOT USE IN PRODUCTION
https://docs.djangoproject.com/en/1.8/howto/static-files/
Adding this to urls.py
did the trick
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
回答3:
In your Nginx Config did you try to set:
location /static {
instead of
location /static/ {
also make sure the user running nginx has read permissions to the static folder.
来源:https://stackoverflow.com/questions/33051578/static-files-not-being-displayed-in-production