Serving / out of static directory, /api for Django DRF services (development/runserver/DEBUG mode)

41 views
Skip to first unread message

LiteWait

unread,
Apr 21, 2015, 3:10:10 PM4/21/15
to django...@googlegroups.com
Planning to host the client side of our application in production from a proxy to an S3 site from Nginx.

The problem is we'd like to mimic this behavior by serving / in Django runserver using a static directory url() entry.

I've read over https://docs.djangoproject.com/en/1.4/howto/static-files/#serving-other-directories but I can't seem to make Django route / to my client directory.

Idea is I have a project directory /client which contains index.html along with all the other files for site, and when I hit http://127.0.0.1:8000/ I want to serve up <projectdirectory>/client/index.html.

Not sure the following will work because I don't think you can't have a STATIC_URL = '/', right?

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns
+= staticfiles_urlpatterns()


This one seems to make more sense, but I am not clear on what URL pattern could pull this off..

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

Bill Freeman

unread,
Apr 21, 2015, 4:02:24 PM4/21/15
to django-users
That may work for most static things.  The question is whether the static server is happy with an empty path, assuming that you're trying to serve "/" this way.  If not, you might add a separate (earlier) pattern of r'^$' that specifies a path in the extra parameters dictionary (where you have 'document_root', and you may want it's document_root to be different to avoid serving the home page at two urls).

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ffdc58f3-e10c-46df-b14f-ec4bc02c0c70%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

LiteWait

unread,
Apr 21, 2015, 10:08:26 PM4/21/15
to django...@googlegroups.com
I have no clue why this works, but I added the /client directory (full path) to STATICFILE_DIRS and...

from django.conf import settings

if settings.DEBUG:
urlpatterns += patterns(
        'django.contrib.staticfiles.views',
url(r'^(?:index.html)?$', 'serve', kwargs={'path': 'index.html'}),
url(r'^(?P<path>(?:js|css|img)/.*)$', 'serve'),

Now /client/index.html is served up fine, as well as Django normal routes like /admin, /api, etc. I really wish I understood this better.

LiteWait

unread,
Apr 22, 2015, 9:28:41 AM4/22/15
to django...@googlegroups.com
Well, this doesn't work completely. 

Consider the (static) tree:

/client
      index.html
      /apps
          another.html
          /css
              style.css
          /js
              my.js

I need to serve this whole static tree out of /.

Bill Freeman

unread,
Apr 22, 2015, 11:07:37 AM4/22/15
to django-users
Are css and js subdirectries of apps as implied by the (as received) indentation of your message?  Note that your "other" url pattern has js, css, and img, but no apps.

Bill Freeman

unread,
Apr 22, 2015, 11:18:30 AM4/22/15
to django-users
By the way, you can test whether the regular expression matches without getting Django involved, allowing for much quicker theories and tests.

$ python
Python 2.7.3 (default, Jun  9 2014, 04:37:23)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> re.match(r'^(?P<path>(?:js|css|img)/.*)$', 'css/style.css')
<_sre.SRE_Match object at 0x7fb34977ddc8>
>>> _.groups()
('css/style.css',)
>>> re.match(r'^(?P<path>(?:js|css|img)/.*)$', 'apps/css/style.css')   # Does not match
>>> re.match(r'^(?P<path>(?:js|css|img)/.*)$', 'apps/another.html')    # Does not match
>>> re.match(r'^(?P<path>(?:apps|js|css|img)/.*)$', 'apps/another.html')
<_sre.SRE_Match object at 0x7fb34977ddc8>
>>> _.groups()
('apps/another.html',)
>>>

Bill Freeman

unread,
Apr 22, 2015, 11:26:00 AM4/22/15
to django-users
And I probably would have gone with:


from django.conf import settings
if settings.DEBUG:
    urlpatterns += patterns(
        'django.contrib.staticfiles.views',
        url(r'^$', 'serve', kwargs={'path': 'index.html'}),
        url(r'^(?P<path>.*)$', 'serve'),

The second url patter above must be the last one overall.  Any of your other patters, for you Django views, for example, have already not matched by the time this one gets tried.

LiteWait

unread,
Apr 22, 2015, 1:26:16 PM4/22/15
to django...@googlegroups.com
Thank you. That is a perfect solution.
Reply all
Reply to author
Forward
0 new messages