2014-04-18 232 views
1

我在開發服務器的Django項目中設置靜態文件時遇到問題。我在Python 2.7.5+中使用Django-1.6.1。在Django中設置靜態文件

我跟着從這個鏈接的instrucions: Managing static files (CSS, images)

所以,我加入django.contrib.staticfiles到INSTALLED_APPS

INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'BlogContent', 
) 

我成立STATIC_URL

STATIC_URL = '/static/' 

而且我也修改我的ur ls.py這樣:

urlpatterns = [   
    url(r'^admin/', include(admin.site.urls)), 
    url(r'^$', home_view), 
    url(r'^about/$', about_view) 
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

在模板我用這個標籤:

{% load staticfiles %} 
<img src="{% static "elo.jpg" %}"/> 

,所有文件都是到project_root /靜態/和運行服務器後,我收到這樣的:

"GET /static/elo.jpg HTTP/1.1" 404 1625 

你有什麼想法如何解決它?預先感謝您的幫助。

回答

1

Django不提供文件本身;它將該作業留給您選擇的任何Web服務器。 so刪除+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)urls.py。使用的Apache2來存儲靜態或媒體文件

https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/#serving-files

在Apache *.conf檔案(Apache 2.4)

<VirtualHost *:80> 
     ServerName example.com 
     ServerAdmin [email protected] 

     Alias /media/ /home/tu/blog/media/ 
     Alias /static/ /home/tu/blog/collected_static/ 

     <Directory /home/tu/blog/media> 
       Require all granted 
     </Directory> 

     <Directory /home/tu/blog/collected_static> 
       Require all granted 
     </Directory> 

     WSGIScriptAlias//home/tu/blog/blog/wsgi.py 

     <Directory /home/tu/blog/blog> 
     <Files wsgi.py> 
       Require all granted 
     </Files> 
     </Directory> 
</VirtualHost> 

如果你使用Apache 2.2使用

Order allow,deny 
Allow from all 

,而不是

Require all granted 

注意:您可以運行apachectl -v看到你的Apache2版本

+0

如果只有一個網站,你可以刪除''標籤 – WeizhongTu

+0

沒有爲我工作。我正在使用一個ProxyPass併爲靜態文件設置了'ProxyPass/static /!',仍然有403. – varagrawal

+0

@varagrawal你可以在別的'ProxyPass/host:8000之前放置'Alias/static// path/to/static' ' – WeizhongTu

0

這裏是一個nginx配置的尖晶石,爲靜態文件&代理稱爲APP應用程序:

http { 
… 
    server { 
     … 
     location /static/ { 
      autoindex on; 
      alias /usr/share/nginx/html/static/; 
     } 

     location /APP { 
      return 301 /APP/; 
     } 

     location /APP/ { 
      rewrite ^/intranet(.*) /$1 break; 
      proxy_redirect off; 
      proxy_pass http://127.0.0.1:8000; 
     } 
    } 
} 

請注意,我用gunicornlocalhost:8000上運行我的應用程序,但我使用http://localhost/APP連接到它。