Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override django static file development server?

Tags:

django

I am beginning to use the application 'django.contrib.staticfiles' to collect static files into the /static/ directory of my project.

The problem is that, when I am using the django development server (manage.py runserver) it automatically serve static files.

It is usually fine, but in my case, I would like to serve these static files myself.

I would like to put in the urls.py file something like that :

urlpatterns += patterns('',
        url('^static/(?P<path>.*)$', myStaticMediaServe,{'document_root': settings.STATIC_ROOT ,'show_indexes': True}),
        )

The problem is that 'django.contrib.staticfiles' application has got the priority on '/static/' url when settings.DEBUG=True : I cannot find a way to make Django use my '/static/' urlpattern description while being in debug mode

If I remove 'django.contrib.staticfiles' from settings.py : my '/static/' urlpattern works but I loose static files collecting.

Do you have an idea to use 'django.contrib.staticfiles' AND use my own static files server through an urlpattern description AND have settins.DEBUG=True

like image 255
Eric Avatar asked May 29 '12 16:05

Eric


People also ask

Can Django serve static files in production?

During development, as long as you have DEBUG set to TRUE and you're using the staticfiles app, you can serve up static files using Django's development server. You don't even need to run the collecstatic command.

What does Collectstatic do in Django?

Django also provides a mechanism for collecting static files into one place so that they can be served easily. Using the collectstatic command, Django looks for all static files in your apps and collects them wherever you told it to, i.e. the STATIC_ROOT .

What is STATIC_URL in Django?

STATIC_URL is simply the prefix or url that is prepended to your static files and is used by the static method in Django templates mostly. For more info, read this. STATIC_ROOT is the directory or location where your static files are deployed when you run collectstatic .


1 Answers

I found that, by default, django 'runserver' itself, preempts /static/ urls : even with custom middleware, you cannot force django to point '/static/' to your code.

The only solution I found : use --nostatic option for './manage.py runserver', then one can use his own url patterns and views in order to serve static files.

like image 109
Eric Avatar answered Oct 01 '22 08:10

Eric