我真的很困惑什么时候应该使用这些,
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
答案 0 :(得分:0)
如果您希望 Django 在开发模式下提供静态文件,则添加 static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
,如果希望 Django 提供媒体文件,则添加 static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
em> 处于开发模式。
如果您想同时提供两者,您可以使用:
urlpatterns = [
# … the rest of your URLconf goes here …
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
在生产中(当 DEBUG = False
时),Django 不提供静态/媒体文件,在这种情况下,您应该配置网络服务器(apache、nginx 等)来提供这些文件.或与 WhiteNoise 合作。