django - 如何在 django 中的自定义 url 处提供静态文件目录

标签 django static-files

我正在开发一个 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/

相关文章:

python - Django 删除前导零

python - 启用 WhiteNoise 时,“collectstatic”命令失败

heroku - 从私有(private) AWS S3 存储桶在 Flask 中提供静态文件

python - 如何将 PIL `Image` 转换为 Django `File` ?

python - pip 安装django-allauth UnicodeDecodeError

django - django admin admin.TabularInline有多个ForeignKey

python - kombu.exceptions.EncodeError : [<Vkwallpost: Vkwallpost object> . .. 不是 JSON 可序列化的 - 仅在本地主机上

amazon-web-services - 如何在AWS托管的Docker容器内通过Spring Boot加载静态文件?

自托管 OWIN WebAPI 上的 SSL 加密

javascript - Express 不提供整个静态目录,仅提供index.html