python - Django静态文件问题

标签 python django

Django staticfiles app help 的重复

我正在使用 Django 1.3 beta,静态文件应用程序很困惑。在开发模式下,它旨在自动从 STATIC_URL 路径提供文件。

From http://docs.djangoproject.com/en/dev/howto/static-files/

If you're using the built-in development server (the runserver management command) and have the DEBUG setting set to True, your staticfiles will automatically be served from STATIC_URL in development.

这似乎不起作用,所以我尝试了一个路由到 static.serve View 的 url 模式('/static/')。这只是 404。不知何故,它与 STATIC_URL 冲突,如果我将其更改为“assets/”,它将很好地从静态提供文件。使用“/static”作为静态 url 是合乎逻辑的,但这会发生冲突。

网址模式:

urlpatterns = patterns('',
    # Serve static files for *development only*
    (r'^static/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': settings.STATIC_ROOT}),

静态文件设置:

STATIC_ROOT = '/home/dave/static/flux'

# URL that handles the static files served from STATIC_ROOT.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

理想情况下,我希望 Django 在开发中使用静态 url 来提供文件,而不必使用任何 urlpatterns。

最佳答案

如果您想在使用内置 Django 服务器时提供静态文件,您将需要添加 urlpattern。这就是我所做的(在所有其他模式之后添加此内容:

if settings.DEBUG:
    urlpatterns += patterns('',
            (r'^static/(.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_PATH}),
    )

关于python - Django静态文件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4730716/

相关文章:

python - 如何将字典存储到数据库中,例如键和值对在django中具有单独的字段

python - 如何在列表中添加n个连续元素

python - numpy - 创建二维矩阵的问题

python - 如何按 parent 对路径列表进行分组?

python - 在 Django 中对日期进行分组

python - Django:带有附加字段的CreateView?

python - Django 模型和 PostgreSQL 货币类型

python 帮助!如果/否则语句

Python:使用返回值查找错误原因

python - Django 登录所需的中间件不重定向