python - ./manage.py test 不提供静态文件?

标签 python django-testing django-tests

我在一个项目中发现了以下代码,删除它会使测试失败:

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 when DEBUG is set to True (see django.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/

相关文章:

python - 重新排列 36 位数字?

python - Doctest python 中的私有(private)方法

django - 在 django 1.6 之后运行所有测试

django - Django 中单元测试的 auth_token

django - 在 Django 中使用非托管表运行测试

django - 在 Django TestCases 中呈现模板时缺少静态文件 list 条目

python - 在 gunicorn 上运行时如何使用 python 的 cProfile 来分析 django 应用程序

Django 测试 client.get() 返回 302 代码而不是 200

django - 如何测试在 Django 中有外键的模型?

Python:根据值/条件创建子列表