Django Round-Up podcast and a few links


Yesterday I started listening to the Django Round-Up Podcast hosted by @kennethlove and @bkonkle and it’s really good. I’ve just finished listening to the second episode and I have a bunch of links I wanted to share here.

These are projects I just heard about on the podcast and will definitely try soon. Let me know what you think.

wdb – Web Debugger
wdb is a full featured web debugger based on a client-server architecture.

This is a simple Django middleware utility that allows you to properly serve static assets from production with a WSGI server like Gunicorn.

Put your website into read-only mode for maintanance. It blocks any POST requests and signs users out.

An way to globally disable writes to your database. This works by inserting a cursor wrapper between Django’s CursorWrapper and the database connection’s cursor wrapper. So many cursor wrappers!


Django project layout for Heroku

By: José Padilla

At Blimp we love working with Django. We use it for almost all of our projects, including Blimp and -M.

In Django 1.4, the startproject and startapp commands came with a new template option. Specifying this --template option lets you use a custom project directory structure for your project from a path or URL. By default when you run the startproject management command, it creates a Django project directory structure that contains and a project package (containing a and other files) based on this default template.

We decided to create a project template with the things we always end up doing on our Django projects. This template has a couple of useful settings and Fabric commands that make deploying, managing, and using Django with Heroku easier.

How to install startproject --template= --extension=py,md,dev project_name

What’s included?

  • Fabric – Fabric is a Python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.
  • South – schema and data migrations for Django
  • dj-database-url – Django utility allows you to utilize the DATABASE_URL environment variable to configure your Django application.
  • django-extensions – Custom management extensions for the Django
  • gunicorn – Simply implemented, light on server resource usage, and fairly speedy Python WSGI HTTP Server.
  • psycopg2 – PostgreSQL database adapter for Python

Fabric Script
Fabric is definitely the easiest way to execute local and remote shell commands. We’ve created quite a few of these to help with deployment, database management, and Heroku, all aware of the environment, so for example, you can easily run database migrations in development, staging, or production.

You can keep track of the project on GitHub. Follow me (jpadilla) on GitHub.


How to fix “foreman’s missing output issue” on Python projects

Since we started using Heroku for most of our projects, foreman has been part of our toolkit. We figured out that if we were going to be using it on the server we might as well use it on our development environments. The only problem that we found was that we were not getting all the output from our apps on the terminal. While hacking on a piece of code I’m used to inserting simple ‘print’ statements until I get what I need. With foreman this was not working.

The solution is very simple. Just create a .env file next to your Procfile, and add this environment variable to the first line PYTHONUNBUFFERED=true That’s it, the next time you run your project with foreman you will see all the output.

You can use that .env file to put all the environment variables you need while running your project. Foreman will take care of setting them up for you. Also know that there’s a plugin for the Heroku CLI that makes easy to grab your applications config vars, and place them in your local .env, and vice versa.

My “other” solution. AppleScript?
Before I found the PYTHONUNBUFFERED=true solution, I was using an AppleScript to start my web projects. I must admit that I still use this for some projects, because it’s very fast.

The script does one simple thing very fast and very well. It opens iTerm2 and creates splits for everything I need to run a project. For the most complex project I have it run the Django development server, Redis, Celery and Brunch. I also create an extra panel ready for doing git commits on the project. The best part is that I can run the command using Alfred (with the run command) which makes it event better. Here’s a video of the script doing it’s thing on a simpler project.

Sometimes I wondered why no one has written a Procfile parser in AppleScript. Here’s the AppleScript I stitched together from various places online.

launch "iTerm"

tell application "iTerm"
	set myterm to (make new terminal)
	tell myterm
		launch session "DevelopmentEnvironment"
		# Start Django Server
		tell the last session to write text "workon lift && ./ runserver_plus --settings=lift.settings.development"
		tell the last session to set name to "Django Server"
		# Start Brunch
		tell i term application "System Events" to keystroke "d" using {command down}
		tell the last session to write text "workon lift && fab brunchwatch"
		tell the last session to set name to "Brunch"
		# Panel for git
		tell i term application "System Events" to keystroke "d" using {command down, shift down}
		tell the last session to write text "workon lift"
		tell the last session to set name to "git"
	end tell
end tell

Want to learn Django? Start here.

Today I read a post on HN called Want to learn Rails? Start Here and it seemed like a great idea so I decided to do the same for Django.

This is my collection of web resources for folks who are just getting started with Django. The list contains what I believe is the absolute essential for the total n00b. I added a link to more than 170 videos on various Django topics as a bonus just to give newcomers an idea of how much information about Django it’s out there. What are you waiting for? Get started.

The official Django tutorial – Writing your first Django app

Getting started with Django on Heroku – The easiest way to deploy your projects and free hosting

The Django Book – It’s a bit outdated but still a great place to get started

Django Best Practices – Here you can learn some really good tips on how to organize your projects

170+ Django conference videos – Everything and anything, great resource

Finally a cool tip I just found out about. If you are looking for information about any object in the Django framework you can just type in your browser<any object here> and you will be redirected to the proper page in the official documentation. Try it


Django Static Files on Heroku

It’s a fact. Heroku is a great solution for Python web developers. They are the best and truly polyglot hosting solution for web apps. The only thing that took me a while to get working with my Django apps was the static files. I started using S3 as my backend but preferred a simpler solution for very small projects.

I searched around and found a very simple solution.

web: python app/ collectstatic --noinput; bin/gunicorn_django --workers=4 --bind=$PORT app/ 

Just add the collectstatic command to the Procfile.