Setup Django with Passenger Prefpane

I am loving Django for web development. I didn’t have it set up to serve my (development) projects automatically until just now.

I had installed the Passenger Prefpane, which greatly simplifies the management of VirtualHost-based serving of sites, at least for Rails and other ruby-based frameworks. With a little work, you can use the same setup to serve Django projects.

Rather than re-detail the setup, I’ll just point you to the mod_passenger setup, and the Passenger Prefpane setup pages.

Now, to set up a Django project: obviously you need a django project. Create one, and note where it is located. I stick all of mine in ~/Sites.

Add a file to the root of this project, called passenger_wsgi.py. It needs to contain the following data:

1 import os, sys
2 sys.path.append('/Users/matt/Sites') # Replace with your directory
3 os.environ['DJANGO_SETTINGS_MODULE'] = 'testing.settings' # replace with your projectname.
4 import django.core.handlers.wsgi
5 application = django.core.handlers.wsgi.WSGIHandler()

Now, add the site to the Passenger prefpane. My site is the testing.local site:

Now visit the address and ensure that it works. You should get the basic you need to set up django message.

To get the admin media served by the standard apache setup, I created a link inside the /Library/WebServer/Documents directory to /Library/Python/2.5/site-packages/django/contrib/admin/media. This can be done inside the Terminal:

sudo ln -s /Library/Python/2.5/site-packages/django/contrib/admin/media /Library/WebServer/Documents/admin-media/

Then, change the ADMIN_MEDIA setting in your django projects to http://localhost/admin-media/. This is probably the weakest point in the setup, as it will only work for pages served to your machine, not others on your network.

1 # URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
2 # trailing slash.
3 # Examples: "http://foo.com/media/", "/media/".
4 ADMIN_MEDIA_PREFIX = 'http://localhost/admin-media/'

I had some issues with mod_passenger serving the data for localhost (and arne, my actual machine’s name) from the first installed VirtualHost. To overcome this, I put in a new file into /etc/apache2/other/localhost.conf, which looks like:

1 <VirtualHost *:80>
2   DocumentRoot "/Library/WebServer/Documents"
3   <directory "/Library/WebServer/Documents">
4     Order allow,deny
5     Allow from all
6   </directory>
7 </VirtualHost>

This forces unnamed, or other sites to work as intended. Including the /User/*/Sites directories.

Prowl

Prowl is awesome. Growl notifications can be forwarded to your iPhone.

But you can get notifications from anywhere. A Perl script is included, but that didn’t work on my server. So I wrote one in Ruby:

 1 #! /usr/bin/ruby
 2 
 3 # A ruby class for sending notifications to Prowl.
 4 
 5 require 'uri'
 6 require 'net/http'
 7 require 'net/https'
 8 
 9 class String
10   def urlencode
11     gsub( /[^a-zA-Z0-9\-_\.!~*'()]/n ) {|x| sprintf('%%%02x', x[0]) }
12   end
13 end
14 
15 class Hash
16   def urlencode
17     collect { |k,v| "#{k.to_s.urlencode}=#{v.to_s.urlencode}" }.join('&')
18   end
19 end
20 
21 class Prowler
22   def initialize user, pass
23     @url = URI.parse('https://prowl.weks.net/api/add_notification.php')
24     @username = user
25     @password = pass
26     
27     @http = Net::HTTP.new(@url.host, @url.port)
28     @http.use_ssl = true
29   end
30 
31   def send_notification app, evt, desc
32     
33 
34     options = {
35       'application' => app,
36       'event' => evt,
37       'description' => desc
38     }
39     
40     req = Net::HTTP::Get.new("#{@url.path}?#{options.urlencode}")
41     req.basic_auth @username, @password
42     @http.request(req)
43   end
44 
45 end
46 
47 # How to use?
48 # p = Prowler.new('username', 'password')
49 # p.send_notification('App','Event','Desc')