0

Community,

I am using Django 1.7. Due to specific nature of the project, I have complicated needs for handling static files. In other words, I need /static/ to be served on development just as usual, but /static/blueprints/ subdirectory should be served by a custom view.

It seems anyway that static serving view has priority above everything in urls.py. The following just does not work:

urlpatterns = patterns('',
    url(r'^static/blueprints/(?P<blueprint>[\w-]+)/(?P<path>.+)', 'my_view_name'),
    ...
)

The view never gets fired when accessing an appropriate URL. But changing static/blueprints to, say, my_static/blueprints makes this path to work, so the view actually does work.

Of course, I need that only on development; on production, a script will be used to form the necessary directory structure served by Nginx. So solutions:

  1. Avoid using debug=True on devserver. But Django's debugger is very useful.
  2. Avoid using django.contrib.staticfiles. Not pleasing too, I'd like to use the collectstaic command.
  3. Use just /blueprints/ path instead of /static/blueprints/, which is a solution, but just against aesthetics.

Is there any more elegant solution?

Anatoly Scherbakov
  • 1,672
  • 1
  • 13
  • 20
  • `my_view_name`? or how you suppose view makes this path to work? https://github.com/django/django/blob/master/django/views/static.py#L21 – madzohan Nov 10 '14 at 07:39
  • Well, `my_view_name` is actually `myapp.views.blueprint_view`, which is my own view. And it actually employs the view you linked to, just adds some custom logic. – Anatoly Scherbakov Nov 10 '14 at 07:42
  • So I don't know what that view actually does, but while using `serve` you must provide the ``document_root`` param and where the `$` in the end of your pattern? `url(r'^static/(?P.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),` – madzohan Nov 10 '14 at 07:54
  • I understand that. I do not use `serve` in here; if I would, that would not be a problem, I'd just added an item to `STATICFILES_DIRS`. I have my own view which has its own logic of serving static files from multiple directories. And it does its job, as I have noted in the question. The problem is that this view URL is overwritten by Django's own static serving logic. – Anatoly Scherbakov Nov 10 '14 at 08:09

1 Answers1

1

You're using ./manage.py runserver on your dev server, right? Then you can go this way: ./manage.py runserver --nostatic, and in urls.py:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = patterns('',
    url(r'^static/blueprints/(?P<blueprint>[\w-]+)/(?P<path>.+)', 'my_view_name'),
    ...
)
if settings.DEBUG:
    urlpatterns += staticfiles_urlpatterns()
ZZY
  • 3,689
  • 19
  • 22