Posts for Django

Setting up a template_postgis on Lucid

I wasn't able to find instructions in the Django docs for setting up a template_postgis database with postgis-1.5 and Postgres 8.4 on Ubuntu Lucid (10.04). Below is what worked for me. GeoDjango installed via Ubuntu 10.04 packages #!/usr/bin/env bash POSTGIS_SQL_PATH=`pg_config --sharedir`/contrib createdb -E UTF8 template_postgis # Create the template spatial database. createlang -d template_postgis plpgsql # Adding PLPGSQL language support. psql -d postgres -c "UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis';" psql -d template_postgis -f $POSTGIS_SQL_PATH/postgis.sql # Loading the PostGIS SQL routines psql -d template_postgis -f $POSTGIS_SQL_PATH/spatial_ref_sys.sql psql -d template_postgis -c "GRANT ALL ON geometry_columns TO PUBLIC;" # Enabling users to alter spatial tables. psql -d template_postgis -c "GRANT ALL ON spatial_ref_sys TO PUBLIC;" GeoDjango built from source Use the above script, but change POSTGRIS_SQL_PATH to: POSTGIS_SQL_PATH=`pg_config --sharedir`/contrib/postgis-1.5 Make sure to run sudo ldconfig after compiling everything, or else postgis won't be able to find geos. You'll see errors like this: ...

(view comments) (read more)

Provisioning a new Ubuntu server for Django

I've been a long-time satisfied user of Webfaction, but recently I've had a strong urge to move to VPS hosting so that I can have greater control over the environment. After some research, I went with Rackspace Cloud because of the incredibly cheap low-end options. My site doesn't use a huge amount of bandwidth, so Rackspace looks to be the most feature-packed and still cost-effective option. A friend of mine, Kevin Whitaker, recently posted a great article about getting up and running with Django in a server environment for testing or production. He used Ubuntu, Postgres, Nginx, and FastCGI to make up his stack. I've never set up Nginx before, so his post was a great help in getting Nginx configured. My stack is slightly different, however, since I prefer to use Gunicorn instead of FastCGI and I use supervisord to manage my processes. I also use virtualenv to manage ...

(view comments) (read more)

Using virtualenvwrapper to start processes and swap config files

I do my Django development work locally on OS X, so I have several different daemons installed on my machine. I used to keep Postgres, MySQL, lighttpd, memcached, and more set up to autostart and run continuously, but I didn't like the burden on performance (real or imagined). Also, I switch between projects frequently and I often need to switch config files based on the project I'm about to work on. My solution is to use the end-user customization hooks that Doug Hellmann's excellent virtualenvwrapper provides. If you're not familiar with virtualenvwrapper, it makes it easy to manage your virtualenvs in one place and switch between them quickly and conveniently. The project provides several scripts that are meant to be added to by the end-user to customize behavior. Configuration For my configuration files, I create multiple versions with the changes needed for each project, and I then create a symlink ...

(view comments) (read more)

Snippet: Django Columns Filter

I was looking around for an easy way to split lists of items into columns in a way that the number of items in each column would be less than or equal to the number of items in the first column. This prevents a final column with several more items than the others. I came across an old post from Annie Mullin that established a templatetag to do this using a generator. I liked the idea, but felt that a templatetag was too unwieldy - a filter would be simpler and easier to use in a template. So, I turned it into one: @register.filter def columns(lst, cols): """ Break a list into ``n`` lists, typically for use in columns. >>> lst = range(10) >>> for list in columns(lst, 3): ... list [0, 1, 2, 3] [4, 5, 6] [7, 8, 9] """ try: cols = int(cols) lst = list(lst) except ...

(view comments) (read more)

Django-Reporter

This week I finished up the initial release of Django-Reporter, my first open-source project based on work I've done for my full-time employer, Pegasus News. At Pegasus we send daily, weekly, and monthly email reports out to several people. We have a quite complex codebase, so we need these reports to be as flexible as possible. Limitations of the old way Previously, we were creating one-off executable report scripts and collecting them in a directory on the main web server to be hit by cron periodically. This involved a lot of boilerplate code and duplication, and became difficult to manage long-term - especially when we switched from a single-site to a multi-site structure and a lot of the reports broke. Also, we had to include different DJANGO_SETTINGS_MODULE values depending on the site the report was for, and that filled up the crontab with a lot of verbosity. Classes, registration, and ...

(view comments) (read more)

Creating a Personal PyPi with Chishop

At Pegasus News, we run custom deploy scripts that use pip to read through a requirements.txt file and keep our virtualenvs up to date. We use quite a few 3rd-party Django apps that we pull from PyPi, along with several apps - both internal and 3rd-party - from Github and Bitbucket. I'm a huge fan of virtualenv and pip and I love managing our environments with these tools. When we need to add a new requirement, we simply add it to our requirements.txt and on the next deploy pip will grab it for us on each of our web nodes. The Problems We ran into a couple of issues along the way, however, and I found that a local PyPi server solved them quite nicely for us. Stuck in Downtime Limbo Before we started running a local PyPi server, when PyPi, Github, or Bitbucket went down we couldn't deploy - ...

(view comments) (read more)