2013-10-15 49 views
0

我是Django的新手,對此非常困惑。Django 1.5不會在開發中獲取靜態文件

以下是有關部分在settings.py:

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

STATIC_URL = 'http://http://127.0.0.1:8000/static/' 

STATICFILES_DIRS = (
    PROJECT_ROOT+'/static/') 

TEMPLATE_DIRS = (
    PROJECT_ROOT + '/templates/') 

我的項目文件的結構是這樣的:

{% load staticfiles %} 
{% load i18n %} 
<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
    <title>Bootstrap, from Twitter</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <meta name="description" content=""> 
    <meta name="author" content=""> 
    <!-- Le styles --> 
    <link rel="shortcut icon" href="http://twitter.github.com/bootstrap/assets/ico/favicon.ico"> 
    <link href="{{ STATIC_URL }}css/bootstrap.min.css" rel="stylesheet"> 
    <style type="text/css"> 
     body { 
     padding-top: 60px; 
     padding-bottom: 40px; 
     } 
    </style> 
    <link href="{{ STATIC_URL }}css/bootstrap-responsive.min.css" rel="stylesheet"> 
    </head> 

    <body> 

但:

MyProj 
    manage.py 
    MyProj 
     settings.py 
     urls.py 
     templates 
     base.html 
     static 
     css 
     js 

在base.html文件開頭django沒有得到任何靜態文件:

[15/Oct/2013 05:36:06] "GET /accounts/login/ HTTP/1.1" 200 3402 
[15/Oct/2013 05:36:06] "GET /static/css/bootstrap.min.css HTTP/1.1" 404 2089 
[15/Oct/2013 05:36:06] "GET /static/css/bootstrap-responsive.min.css HTTP/1.1" 404 2122 
[15/Oct/2013 05:36:06] "GET /static/js/jquery.min.js HTTP/1.1" 404 2074 
[15/Oct/2013 05:36:06] "GET /static/js/prettify.js HTTP/1.1" 404 2068 
[15/Oct/2013 05:36:06] "GET /static/js/bootstrap.min.js HTTP/1.1" 404 2083 
[15/Oct/2013 05:36:06] "GET /static/css/bootstrap.min.css HTTP/1.1" 404 2089 
[15/Oct/2013 05:36:06] "GET /accounts/login/ HTTP/1.1" 200 3402 
[15/Oct/2013 05:36:06] "GET /static/css/bootstrap-responsive.min.css HTTP/1.1" 404 2122 
[15/Oct/2013 05:36:19] "GET /static/css/bootstrap.min.css HTTP/1.1" 404 2089 
[15/Oct/2013 05:36:20] "GET /static/css/bootstrap.min.css HTTP/1.1" 404 2089 

我讀過Django docs並嘗試了幾種替代設置,但沒有一個能夠幫助解決問題。所以感謝你的提示。

url.spy

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

# Uncomment the next two lines to enable the admin: 
from django.contrib import admin 
admin.autodiscover() 

urlpatterns = patterns('', 
    # Examples: 
    #url(r'^$', 'myproj.views.home', name='home'), 
    # url(r'^myproj/', include('myproj.foo.urls')), 

    # Uncomment the admin/doc line below to enable admin documentation: 
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), 

    # Uncomment the next line to enable the admin: 
    url(r'^admin/', include(admin.site.urls)), 
    url (r'^accounts/', include('registration.backends.default.urls')), 
) 
+0

難道您發佈urls.py ? DEBUG設置爲True嗎? – esauro

+0

已添加urls.py。是的,DEBUG被設置爲True。 – supermario

回答

1

Staticfiles在Django似乎總是迷惑人誰是剛剛起步,和海事組織的文檔可以改進。

這裏是一個什麼樣的一個快速細數一下:

MEDIA_ROOT:基本目錄,文件上傳表單或模型的形式去的一部分。 File/ImageField的upload_to屬性附加到此路徑。

MEDIA_URL:位於MEDIA_ROOT中的文件的URL模式可通過。

STATIC_ROOT:保存要在生產服務的靜態文件的目錄。

STATIC_URL:文件位於STATIC_ROOT中的URL模式可通過。

STATICFILES_DIRS:導演(Y/IES)持有靜態文件在發展送達。

當您在INSTALLED_APPS中有staticfiles應用程序時,它將僅在DEBUG = True時提供位於STATICFILES_DIRS中的靜態文件。

當您運行$ python manage.py collectstatic時,它將爲您的應用程序和任何第三方應用程序收集任何靜態目錄,並將它們複製到STATIC_ROOT

因此,要在生產中修復靜態文件的路徑,請確保您的設置是正確的,並且在部署之前或作爲部署過程的一部分運行collectstatic

也有靜態文件的新模板標籤:

{% load i18n %} 
{% load static from staticfiles %} 
<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     ... 
     <link href="{% static 'css/bootstrap.min.css' %}" 
      rel="stylesheet"> 
     <link href="{% static 'css/bootstrap-responsive.min.css' %}" 
      rel="stylesheet"> 
    </head> 
    <body> 
     ... 

在你的項目結構,重新命名爲「/靜」到「/靜態資產」,並設置該目錄中STATICFILES_DIRS

將您的STATIC_ROOT更改爲「/ static」,並且不要自己放置任何目錄或文件。讓collectstatic管理命令執行該操作。

這是我通常如何構建一個項目:

/my_app/ 
    /my_app/ 
     __init__.py 
     settings.py 
     urls.py 

    manage.py 

    /another_app_module/ 
     __init__.py 
     models.py 
     ... 
    /static/ 
    /static-assets/ 
     /css 
     /js 
     /images 
    /templates 

鑑於這種文件結構,並假設我的項目在位於:/home/btaylor/django-projects/my_app/我的設置是:

STATIC_ROOT = '/home/btaylor/django-projects/my_app/static' 
STATIC_URL = '/static/' 
MEDIA_ROOT = '/home/btaylor/django-projects/my_app/static/uploads' 
MEDIA_URL = '/static/uploads/' 
STATICFILES_DIRS = ('/home/btaylor/django_projects/my_app/static-assets',) 
+0

感謝您的明確解釋。但我仍然在努力將其應用於路徑。根據上面的文件結構欣賞一些真實的例子。 – supermario

+0

我爲您添加了項目目錄結構和示例HTML。 – Brandon

+0

對不起,我得到'錯誤配置:您的STATICFILES_DIRS設置不是元組或列表;也許你忘了一個尾隨的逗號? '當我運行collectstatic時。什麼應該是STATICFILES_DIRS? – supermario