我在一个项目中发现了以下代码,删除它会使测试失败:
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 whenDEBUG
is 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.
关于python - ./manage.py test 不提供静态文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34295003/