2014-04-18 229 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连接到它。