简体   繁体   English

Django-urls.py中的静态文件夹映射

[英]Django - static folder mapping in urls.py

I have following files structure in my django app 我的Django应用程序中有以下文件结构

|-myapp
|---static
|-----adminapp
|-----clientapp
|-----commonapp
|-----css
|-----fonts
|-----js
|---templates
|-----administration
|-------index.html
|-----client
|-------index.html

It's because I want to build two angular apps, one for users ( static/clientapp ) and one for admin ( static/adminapp ). 这是因为我要构建两个有角度的应用程序,一个用于用户( static/clientapp ),另一个用于admin( static/adminapp )。

templates/administration/index.html and templates/client/index.html are similar and contains code like: templates/administration/index.htmltemplates/client/index.html相似,并且包含如下代码:

<head>
    <script src="static/js/angular.js"></script>
    <script src="static/adminapp/app.js"></script>
</head>
<body ng-app="adminapp"></body>

Links to my admin and client apps should be: 指向我的管理员和客户端应用程序的链接应为:

http://example.com/administration/  
http://example.com/client/

The question is what entry should I add to urls.py to let both angular apps access to the same static folder. 问题是我应该在urls.py添加什么条目,以允许两个角度应用程序访问同一static文件夹。

It must by something like: 它必须类似:

url(r'^administration/static/$', STATIC_ROOT),
url(r'^client/static/$', STATIC_ROOT)

but I'm django newbie and I don't know how to define it correctly. 但我是django新手,我不知道如何正确定义它。

In production you should not let Django handle the static files, so it should by in your nginx/appache config to set the correct paths for administation/static/ and client/static/ . 在生产环境中,您不应让Django处理静态文件,因此应在您的nginx / appache配置中为administation/static/client/static/设置正确的路径。

For dev purposes, you could add something like this: 出于开发目的,您可以添加以下内容:

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}),
   ]

Then you only have to define ADMIN_STATIC_ROOT and CLIENT_STATIC_ROOT in your settings. 然后,您只需在设置中定义ADMIN_STATIC_ROOTCLIENT_STATIC_ROOT

The solution was to use / in urls to static resources. 解决的办法是在静态资源的URL中使用/

eg 例如

<script src="/static/js/angular.js"></script>

Then app base url has no matter. 然后,应用程序基本URL无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM