【问题标题】:In django, can I have multiple instances of static app?在 django 中,我可以有多个静态应用程序实例吗?
【发布时间】:2012-05-25 06:25:02
【问题描述】:

静态应用是否可以有多个实例?

我有不同类型的资源,我可能想从不同的STATIC_URL 提供服务:

  • 我想从/static/ 提供 CSS/JavaScript
  • 我想提供来自/documents/的PDF文档

【问题讨论】:

    标签: django web-services apache django-staticfiles


    【解决方案1】:

    我觉得你对contrib.staticfiles的目的有点误会了:

    在以前的 Django 版本中,放置静态资源是很常见的 在 MEDIA_ROOT 以及用户上传的文件中,并在 媒体网址。引入 staticfiles 应用程序的部分目的是 更容易将静态文件与用户上传的文件分开 文件。

    PDF 文档可能是用户生成的,因此理想情况下它们不会与您的静态文件(css、js)并排存储,而是上传到MEDIA_ROOT

    在生产环境中,您必须 configure your web server 为特定 url 下的目录提供服务。

    在开发过程中,您可以使用 django.views.static.serve to serve different dirs with different url prefixes

    【讨论】:

      【解决方案2】:

      是的,有可能。

      我正在使用urls.py

      if settings.DEBUG:
          urlpatterns += patterns('',
              (r'^docs/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/hdblog/docs'}),
              (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/hdblog/static'}),
          )
      

      你应该看看https://docs.djangoproject.com/en/dev/howto/static-files/?from=olddocshttps://docs.djangoproject.com/en/dev/howto/static-files/#serving-other-directories

      【讨论】:

        猜你喜欢
        • 2013-05-29
        • 2011-05-02
        • 2011-07-12
        • 1970-01-01
        • 1970-01-01
        • 2020-07-06
        • 2021-01-19
        • 2018-07-10
        • 1970-01-01
        相关资源
        最近更新 更多