0

when develop, django can handle static files by changing settings.py, and I suppose it won't cost much resources to handle those static files.

But when apply the project to productive environment using mod_wsgi, the recommended way is to handle static files is to do that with apache's configuration file, and it's even not possible to handle static files within django.

So why don't django handle static file, at least give the users an easy way to do that?

iloahz
  • 4,491
  • 8
  • 23
  • 31

3 Answers3

2

You don't really want Django to serve static files from a performance standpoint. There's no reason to send a request to the Django instance and then have Python return the file when the web server is much faster at doing this on its own.

jncraton
  • 9,022
  • 3
  • 34
  • 49
1

Django isn't meant to serve static files in production.

Django's code that serves static files is meant for development, it is not meant to be performant or even secure.

The primary use of an HTTP server is to serve static files, it is ment to be performant and secure for that. Django has no good reason to interfere between the HTTP server and the file system.

A system administrators is ment to configure a secure and performant HTTP server. It is not the fault of Django if a system administrator cannot do this.

By doing this, Django complies with several Unix principles:

  • Rule of Simplicity: Design for simplicity; add complexity only where you must.
  • Rule of Parsimony: Write a big program only when it is clear by demonstration that nothing else will do.
  • Rule of Modularity: Write simple parts connected by clean interfaces.
  • Rule of Separation: Separate policy from mechanism; separate interfaces from engines.
  • Rule of Clarity: Clarity is better than cleverness.

etc ...

Django's staticfiles system is awesome, in fact many have been using it before it was included into django (see django-staticfiles app). I am convinced that anyone willing to do little effort can understand it fully and find it awesome too. I remember it was a little hard for me to understand django-staticfiles, but then it's nothing but a love story.

Apps can embed static files in addition to urls, templates, views and models by just creating a 'static' sub directory.

If you don't understand how django helps at handling static files very well, you can try this article surviving django staticfiles.

jpic
  • 32,891
  • 5
  • 112
  • 113
  • but it would be easier to deploy, especially when performance is not a question, you would want that. – iloahz Nov 19 '12 at 15:54
  • Django's staticfiles system is awesome, in fact many have been using it **before** it was included into django (see django-staticfiles app). I am convinced that anyone willing to do little effort can understand it fully and find it awesome too. – jpic Nov 19 '12 at 15:56
0

You can use it in production easily using

python manage.py runserver 0.0.0.0:80

and the website will work just as in development. Again, it is not recommended.. But works

In a real-world situations, you prefer some other web server to serve static files for many reasons (mentioned in other answers)

YardenST
  • 5,119
  • 2
  • 33
  • 54