Almost two months ago, my managing editor called our web designer and I into a meeting to discuss how we planned to deal with putting together a voters guide for next month’s school board election and the General Election in the fall. We decided we wanted something interactive and encompassing, and I decided I didn’t want to spend all my time creating new voters guides because by the time one election ends, the next one is right around the corner. The solution: O Django, how I love thee.
Today, that Django project - which also happens to be my first, and hopefully the first of many - went live on The News Journal’s website. You can view it here: http://www.delawareonline.com/schoolboardelections.
The thinking right now is that the May school board elections will be our test, and then I’ll tweak and refine it for the General Election, which features a *big* Wilmington mayoral race.
I’ve learned a ton from this project and already have ideas for future projects and how I can make this one better. Here’s the breakdown of how I built it (along with the thanks I owe to some of the folks who helped me along the way) and what I’d like to do to make it better. As always, any feedback would be fantastic because I’m always learning and wanting to improve stuff.
Good models make good projects
When I first started, I must have rewritten the models for this project a dozen times as I experimented and discovered. For those unfamiliar with Django, models define your database tables, a.k.a. the heart of the application. I finally settled on separate models for candidates, election contests, questions we could ask candidates, the candidates answers to those questions and political parties. The joy of those separations is that if we want to add another contest to the Voters Guide, it’s as simple as going into the admin and adding “Wilmington Mayoral Race.” Even better, though, is that we can ask candidates for a particular race one set of questions and other candidates a different set. It’s damn modular and I love it.
Yes, it’s Django, but it’s still Python
At first, I really had to get rid of the mindset that I was building a Django application because I found that I was limiting myself in what I could do. I kept thinking that to implement something, there must be a way to do it in Django and I needed to search the documentation for how to do it.
That was dumb.
Because Django is built with Python, anything that I could dream up in Python could be implemented with Django. I finally kicked my ugly mindset when I was writing views for the project - the code that acts as manager between the database and templates (what you see online). I needed Django to spit out certain candidates with certain information in certain ways and was kicking myself for not being able to find exactly what I wanted to do in the documentation. The Aha! moment came when putting together the candidate profile page and figuring out how to calculate a candidate’s age using his birthday. I didn’t need to return a Django-made object to my template, I could make anything a variable and send it to the template. So, this calculates age and spits it into the template:
today = date.today()
bday = candidate_info.birthday
birthday = bday.replace(year=today.year)
birthday = bday.replace(year=today.year, day=born.day-1)
if birthday > today:
candidate_age = today.year - bday.year - 1
candidate_age = today.year - bday.year
candidate_age = “Not Provided”
Shell access is a man’s best friend
For reasons I won’t really go into here, our corporate IT folks aren’t fans of giving the local guys shell access on servers. Granted, we’re using shared hosting environments and the last thing they want is some schmuck in Delaware screwing something up and crashing projects from newspapers all over the East Coast, but all we needed were the permissions to run some simple Python commands to sync our databases and collect static files and whatnot. I ended up signing up for hosting through Webfaction so we could do some in-house testing on our project without having to call up corporate IT just to restart Apache. It was one of the best decisions I made and proved invaluable in the last week or so as we scrambled to meet deadline and test things.
Last but certainly not least
Big shout-out to the wonderfully talented Justin Myers, who helped me tremendously with this project at the NICAR conference when I was more-or-less just getting started. And a slightly less enthusiastic but equally grateful shout-out to all the people who answer questions on StackOverflow, super helpful folks.