静态文件未显示在实时服务器

时间:2015-09-19 09:12:42

标签: django nginx

运行Django 1.8.4

静态文件一直在本地工作。我已将项目上传到VPS,除静态文件外,一切正常。

Settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),

示例CSS文件在模板中引用

<link href="{% static "css/bootstrap.min.css" %}" rel="stylesheet">

sudo nano / etc / nginx / sites-available / soundshelter

server {
    server_name MYSERVER;

    access_log off;

    location /static/ {
        alias /opt/soundshelter/soundshelter/static/; #this is the valid location
    }

    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"';
    }
}

Urls.py

from django.conf.urls import patterns, url,include
from soundshelterapp import views


urlpatterns = patterns('soundshelterapp.views',
    url(r'^$', views.home, name='home'),
    url(r'^release/(?P<release_id>\d+)$', views.release, name='release'),
    url(r'^genre/(.*)$', views.genre, name='genre'),
    url(r'^label/(.*)$', views.label, name='label_no_country'),
    url(r'^artist/(.*)$', views.artist, name='all_artists'),
    url(r'^recommendations/(.*)$', views.recommendations, name='user'),
    url(r'^personalised$', views.personalised, name='name'),
    url(r'^social/', include('social.apps.django_app.urls', namespace='social')),
    url(r'^login/$', 'login',name='login'),
    url(r'^logout/$', 'logout',name='logout'),
    url(r'^save_release/$', views.save_release, name='save_release'),
    url(r'^unsave_release/$', views.unsave_release, name ='unsave_release'),


    )

2 个答案:

答案 0 :(得分:0)

感谢Hedde van der Heide的解决方案

将STATIC_ROOT设置为工作文件的实际位置

e.g。

STATIC_ROOT =&#34; /var/www/example.com/static /"

https://docs.djangoproject.com/en/1.8/howto/static-files/#deployment

答案 1 :(得分:0)

在您的实时网站中渲染静态文件,您必须在设置文件中添加以下代码。

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

# Static files (CSS, JavaScript, Images)
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'

# Extra places for collect static to find static files.
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

还可以在安装的应用程序中添加它,

INSTALLED_APPS = 
['django.contrib.staticfiles',]