2

I have following files structure in my django app

|-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).

templates/administration/index.html and templates/client/index.html are similar and contains code like:

<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.

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.

1
  • I don't understand your question. It's very unusual to use django's url router for static files. The development server just takes a root folder and then serves static content according to the directory structure there. It's not clear to me what you are trying to do. Commented Feb 6, 2016 at 19:14

2 Answers 2

2

In production you should not let Django handle the static files, so it should be in your nginx/appache config to set the correct paths for administation/static/ and 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.

Sign up to request clarification or add additional context in comments.

1 Comment

@akn , or for any one who might be interested - For some context on why people say "you should not let Django handle static files" or "It's very unusual to use django's url router for static files" - the Django docs on built-in views suggest "This view is not hardened for production use and should be used only as a development aid; you should serve these files in production using a real front-end web server"
0

The solution was to use / in urls to static resources.

e.g.

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

Then app base url has no matter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.