【问题标题】:Serving static files in debug using Django 1.4 is very frustrating使用 Django 1.4 在调试中提供静态文件非常令人沮丧
【发布时间】:2012-05-19 14:50:50
【问题描述】:

我喜欢 Django,但在开发过程中获取静态文件是一件很痛苦的事情。我已经安装了网络资产以使工作更轻松。基本上,我的资产在我当前的配置下是 404,但管理资产很好(?!)。我的项目布局是这样的;

myapp/
    common/
        static/
            bootstrap/
                img/
                less/
                js/
        templates/
        __init__.py
        assets.py
        urls.py
        models.py
        views.py
    projects/
        templates/
        static/
        __init__.py
        assets.py
        urls.py
        models.py
        views.py
    public/ <- (static files collected here)
settings.py

在我的设置中,我配置了以下值

__DIR__ = os.path.abspath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(__DIR__, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(__DIR__, 'public')
STATIC_URL = '/static/'

我的网址是这样配置的;

urlpatterns = patterns('',
    url(r'^projects/', include('myapp.projects.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', include('myapp.common.urls')),
)

urlpatterns += staticfiles_urlpatterns()

同样,assets.py 看起来很标准;

from django_assets import register, Bundle

js = Bundle(
    'bootstrap/js/bootstrap-alert.js',
    'bootstrap/js/bootstrap-button.js',
    'bootstrap/js/bootstrap-carousel.js',
    'bootstrap/js/bootstrap-collapse.js',
    'bootstrap/js/bootstrap-modal.js',
    'bootstrap/js/bootstrap-popover.js',
    'bootstrap/js/bootstrap-scrollspy.js',
    'bootstrap/js/bootstrap-tab.js',
    'bootstrap/js/bootstrap-tooltip.js',
    'bootstrap/js/bootstrap-transition.js',
    'bootstrap/js/bootstrap-typeahead.js',
    output='bootstrap/script.js',
    debug=False
)

register('js', js)

而且我的基础模板非常基础;

{% load assets %}
{% assets 'js' %}
    <script type="text/javascript" src="{{ ASSET_URL }}"></script>
{% endassets %}

所以,当runserver运行时,所有的js文件都被捆绑到1个标签中,下面的url为http://localhost:8000/static/bootstrap/script.js?ed501ad2。但是这个 404 带有消息“'bootstrap/script.js' 找不到”。

但是,如果我登录 /admin 应用程序,那么所有 css 资源都会正确呈现。我运行了 collectstatic 并验证资产确实存在于 public/ 目录中。

我在这里错过了什么?

【问题讨论】:

    标签: python django static-files


    【解决方案1】:

    您是否按照 django-assets 文档中的要求将django_assets.finders.AssetsFinder 添加到您的STATICFILES_FINDERS 中?

    http://elsdoerfer.name/docs/webassets/django/index.html

    【讨论】:

    • 非常感谢!我没有意识到这一点 - 我应该更仔细地阅读文档!
    • 克劳德:我不认为这样的评论有帮助。不是很欢迎 Django 社区。​​span>
    • 丹尼,你是对的,但我刚开始对人们在没有先阅读文档或谷歌搜索的情况下发布关于 SO 的问题感到愤怒,而且这是一种不断增长的趋势。我只是认为这对 SO 社区没有任何好处。在专业环境中,任何人都会因为问这些问题而感到羞耻,并且实际上会在浪费同事的时间之前做功课,我看不出在这里可以做同样的事情。
    猜你喜欢
    • 2016-10-09
    • 2011-07-19
    • 1970-01-01
    • 2012-01-02
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    相关资源
    最近更新 更多