Wednesday, April 22, 2015

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

Thank you. That is a perfect solution.

On Wednesday, April 22, 2015 at 11:26:00 AM UTC-4, ke1g wrote:
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.


On Wed, Apr 22, 2015 at 11:17 AM, Bill Freeman <ke1...@gmail.com> wrote:
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',)
>>>

On Wed, Apr 22, 2015 at 11:06 AM, Bill Freeman <ke1...@gmail.com> wrote:
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.

On Wed, Apr 22, 2015 at 9:28 AM, LiteWait <t...@toogopos.com> wrote:
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 /.


On Tuesday, April 21, 2015 at 10:08:26 PM UTC-4, LiteWait wrote:
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.


On Tuesday, April 21, 2015 at 4:02:24 PM UTC-4, ke1g wrote:
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).

On Tue, Apr 21, 2015 at 1:56 PM, LiteWait <t...@toogopos.com> wrote:
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',}),
 
)

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

--
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/5ba4b639-48df-4c3b-8fb8-00a1a166f58e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/73382bf6-5c26-47a0-90f9-f407cc9934a4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment