我正在开发一个 Django 应用程序,该应用程序具有从 http://host/static 提供的静态文件.
我有一个充满静态 html/js/css 的新文件夹,需要通过 http://host/staff 进行访问。 .
我如何告诉 django 从该 url 提供/staff 文件?
使用 nginx 或其他代理不是一个选项,更改 url 也不是一个选项。
我可以通过添加 urlpatterns += static('/staff/', document_root='staff')
使其部分工作,但这仅在 Debug = True< 时有效
.
最佳答案
修改自 https://docs.djangoproject.com/en/2.1/ref/urls/#static :
from django.conf import settings
from django.conf.urls.static import static
PATH = '/custom-static-files/'
ROOT = '/var/www/html/custom-static-files/'
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(PATH, document_root=ROOT)
ROOT 是您要提供服务的文件夹的路径。它应该是操作系统上文件的绝对路径。
PATH 是将提供文件的路径。如果您的网站是https://example.com并且您设置 PATH = '/custom-static-files/
那么文件将在 https://example.com/custom-static-files/ 处提供
关于django - 如何在 django 中的自定义 url 处提供静态文件目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40248356/