首页 > 解决方案 > How to serve another static root in Django for html pages?

问题描述

The typical configuration serves two static roots:

http://www.example.org/static/
http://www.example.org/media/

This is STATIC_URL and MEDIA_URL.

I would like to add a third one to host static files build with Sphinx:

http://www.example.org/docs/

I know I could configure this on the level of the web server. Is it also possible to configure this on the level of Django?

Here is my python package, that implements Sphinx with Django templates and renders it to static pages. A kind lightweight read-the-docs. Still in an early state, yet working.

https://github.com/elmar-hinz/Django.SphinxCMS

标签: djangopython-sphinx

解决方案


像为静态文件一样构建一个 url 怎么样?该DOCS_ROOT设置应该是您设置中的字符串。

from django.urls import re_path    
from django.views.static import serve
from django.conf import settings

urlpatterns += [
    re_path(r'^docs/(?P<path>.*)', serve, {'document_root': settings.DOCS_ROOT})
]

推荐阅读