我在一个项目中发现了以下代码,删除它会导致测试失败:
if 'test' in sys.argv:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': os.path.join(settings.BASE_DIR, 'viewer/templates/static')}),
)
出于某种原因,./manage.py test 不提供没有此行的静态文件,即使它在 ./manage.py runserver 上运行良好。为什么会这样?
请您参考如下方法:
之所以需要为 manage.py test 指定那些行,是因为提供静态文件不是默认的 django 行为。
如果您通过manage.py runserver 运行服务器,static content is only served if DEBUG=True .
提供此功能是为了在开发模式下为用户提供帮助,不建议在生产中使用。
来自上面的链接:
Serving the files
In addition to these configuration steps, you’ll also need to actually serve the static files.
During development, if you use
django.contrib.staticfiles, this will be done automatically by runserver whenDEBUGis set toTrue(seedjango.contrib.staticfiles.views.serve()).This method is grossly inefficient and probably insecure, so it is unsuitable for production.
See Deploying static files for proper strategies to serve static files in production environments.
