Django 1.3 comes with staticfiles and it’s simple and awesome. Basically it allows you to have your Django app contained in a small folder with everything needed to run/develop/test it. You don’t need to play with Apache, Nginx, Lighttpd or some other webserver. Most importantly, the people that will have access to your code (maybe CSS designers for example) that may not be experienced in Django will have less problems installing it, and you will save time supporting them.
For demonstration purposes, let’s assume you have your django project in/home/yourusername/yourproject/ and an app called yourapp in/home/yourusername/yourproject/yourapp/.
Basically you will:
1) create a folder static in yourapp/ (i.e./home/yourusername/yourproject/yourapp/static/)
2) put ‘django.contrib.staticfiles’ in your INSTALLED_APPS in settings.py (i.e./home/yourusername/yourproject/settings.py)
3) define STATIC_URL in settings.py to ‘/static/’
4) refer to any files in static/ as {{STATIC_URL}} in your templates.
5) runserver (./manage.py runserver)You then put all your CSS/JS and other static resources inside static/. When you’re ready to deploy, you can simply symlink from the deployment folder (e.g. /var/www/) to your django project folder. Alternatively, and probably better, you can use Django’s admin new command collectstatic which basically copies the static files from the development folder to the public folder in the deployment server.
Sunday, 7 August 2011
Django 1.3 basic static file hosting!
After several hours attempting to link external stylesheets as static files with no luck, I finally stumbled on an easy solution to my problem: Location of the static folder!
The static file doc on the Django page didn't quite make that clear to me before I found that page.
Subscribe to:
Posts (Atom)