Django静态文件问题

2024-05-20 07:16:57 发布

您现在位置:Python中文网/ 问答频道 /正文

Django staticfiles app help的副本

我正在使用Django 1.3beta,静态文件应用程序让人困惑。在开发模式中,它意味着自动为来自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.

这似乎不起作用,所以我尝试了一个url模式('/static/'),该模式路由到static.serve视图。这只是404'd。不知怎么的,它与静态的URL冲突,如果我把它改成'assets/'它将从静态的文件服务只是罚款。对静态url使用'/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。


Tags: 文件thedjangocomhttpurl模式静态
1条回答
网友
1楼 · 发布于 2024-05-20 07:16:57

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

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

相关问题 更多 >