Djangopipe理样式表不会在生产服务器上加载

Django的内置pipe理站点的样式表不会被加载到运行Django 1.6的生产服务器上。 我可以看到许多其他人遇到了这个问题,我已经尝试了所有提供给他们的解决scheme,但都没有成功。 我的Django站点在Nginx v.1.2.1代理之后的Gunicorn上运行。

由于我打算构build一个仪表板,并将使用pipe理站点来pipe理我的用户,组和模型,我在我的站点中创build了一个“pipe理”应用程序:

INSTALLED_APPS += ('apps.admin', ) 

我重写和扩展Django的一些pipe理模板,并从django.contrib.admin.templates这里复制它们:

 /www/site/apps/admin/__init__.py /www/site/apps/admin/models.py /www/site/apps/admin/views.py /www/site/apps/admin/urls.py /www/example/apps/admin/templates/admin/base.html /www/example/apps/admin/templates/admin/base_site.html /www/example/apps/admin/templates/admin/index.html /www/example/apps/admin/templates/admin/my_dashboard.html # mine 

这里是我的urls.py文件的相关设置

 from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', # ... url(r'^admin/', include(admin.site.urls)), url(r'^admin/dashboard/$', 'apps.admin.views.dashboard', {'template': 'my_dashboard.html'}, name='dashboard'), ) 

这是我的设置。 我的网站的实际静态文件驻留在这里:

 /www/example/static/css /www/example/static/js 

这是我的静态根。 Django文档说,它应该不同于你的静态文件实际位置。

 STATIC_ROOT = "/var/www/example/static/" 

我的静态url的前缀:

 STATIC_URL = '/static/' 

静态文件和发现者的位置:

 STATICFILES_DIRS = ( '/www/example/static' ) STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', ) 

我的Nginxconfiguration文件:

 upstream gunicorn { server 127.0.0.1:8000 fail_timeout=0; } server { listen 80; server_name www.example.com; rewrite ^/(.*) http://example.com/$1 permanent; } server { listen 80; server_name example.com; root /www/example; client_max_body_size 4G; keepalive_timeout 5; location /static/ { alias /www/example/static/; } location /media/ { alias /var/www/example/media/; } try_files $uri @django; location @django { proxy_pass http://gunicorn; proxy_redirect off; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } error_page 500 502 503 504 /500.html; location = /500.html { root /www/example/static; } } 

我可以看到,该网站是服务器我的静态文件正确。 例如,我可以看到这个图像:

 http://example.com/static/img/small_img.jpg 

但是,当我尝试访问一个Django样式表时,Nginx会产生一个404错误。

 http://example.com/static/admin/css/base.css 

…即使在我的浏览器中查看源代码显示这些链接:

 <link rel="stylesheet" type="text/css" href="/static/admin/css/base.css" /> <link rel="stylesheet" type="text/css" href="/static/admin/css/login.css" /> 

以下是我所做的其他事情:

  • 我确认“django.contrib.staticfiles”和“django.contrib.admin”在INSTALLED_APPS中。
  • 我进入Python shell并validation了我的STATIC_ROOT,STATIC_URL和STATICFILES_DIR设置。
  • 我在我的服务器上运行“collectstatic”命令,并validation它将Djangopipe理站点的静态文件复制到我的STATIC_ROOT。
  • 我尝试添加一个额外的位置指令到我的nginxconfiguration文件,如下所述,并重新启动我的Nginx服务。
  • 我尝试创build一个目录/ www / example / static / admin目录,然后在其中创build一个“静态”符号链接,指向我的虚拟环境目录中的静态目录… / django / contrib / admin / static /。
  • 我使用开发者控制台在Chrome中加载了pipe理员主页,看看是否有什么奇怪的事情发生,但只报告了404错误。

我将通过将Django的静态文件复制到我的站点的静态文件目录并更改我的base.html版本中的hrefs来尝试“monkey patching”。 但是,这不是一个好的解决scheme。

对不起,这么长的post。 谁能告诉我我做错了什么? 除此之外,pipe理员网站似乎正在工作。 这只是不加载的样式表。 谢谢!

它看起来像你的STATIC_ROOT不匹配nginx的location /static/别名。 你错过了/var/