2013-10-15 50 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