我的 django 应用程序中有以下文件结构
|-myapp
|---static
|-----adminapp
|-----clientapp
|-----commonapp
|-----css
|-----fonts
|-----js
|---templates
|-----administration
|-------index.html
|-----client
|-------index.html
这是因为我想构建两个 Angular 应用程序,一个用于用户 (static/clientapp
),一个用于管理员 (static/adminapp
)。
templates/administration/index.html
和 templates/client/index.html
类似,包含如下代码:
<head>
<script src="static/js/angular.js"></script>
<script src="static/adminapp/app.js"></script>
</head>
<body ng-app="adminapp"></body>
指向我的管理和客户端应用程序的链接应该是:
http://example.com/administration/
http://example.com/client/
问题是我应该向 urls.py
添加什么条目,以便让两个 Angular 应用程序访问同一个 static
文件夹。
它必须是这样的:
url(r'^administration/static/$', STATIC_ROOT),
url(r'^client/static/$', STATIC_ROOT)
但我是 django 新手,我不知道如何正确定义它。
最佳答案
在生产中,您不应该让 Django 处理静态文件,因此应该在您的 nginx/appache 配置中为 administation/static/
和 client/static/设置正确的路径
.
出于开发目的,您可以添加如下内容:
if settings.DEBUG:
urlpatterns += [
url(r'^administation/static/(?P<path>.*)$', ' django.views.static.serve', {'document_root': settings.ADMIN_STATIC_ROOT}),
url(r'^client/static/(?P<path>.*)$', ' django.views.static.serve', {'document_root': settings.CLIENT_STATIC_ROOT}),
]
那么您只需在设置中定义 ADMIN_STATIC_ROOT
和 CLIENT_STATIC_ROOT
即可。
关于python - Django - urls.py 中的静态文件夹映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35244341/