tags:

views:

386

answers:

4

I've just completed the very very nice django tutorial and it all went swimmingly. One of the first parts of the tutorial is that it says not to use their example server thingie in production, my first act after the tutorial was thus to try to run my app on apache.

I'm running OSX 10.5 and have the standard apache (which refuses to run python) and MAMP (which begrudgingly allows it in cgi-bin). The problem is that I've no idea which script to call, in the tutorial it was always localhost:8000/polls but I've no idea how that's meant to map to a specific file.

Have I missed something blatantly obvious about what to do with a .htaccess file or does the tutorial not actually explain how to use it somewhere else?

+4  A: 

You probably won't find much joy using .htaccess to configure Django through Apache (though I confess you probably could do it if you're determined enough... but for production I suspect it will be more complicated than necessary). I develop and run Django in OS X, and it works quite seamlessly.

The secret is that you must configure httpd.conf to pass requests to Django via one of three options: mod_wsgi (the most modern approach), mod_python (second best, but works fine on OS X's Python 2.5), fastcgi (well... if you must to match your production environment).

Django's deployment docs have good advice and instruction for all three options.

If you are using the default OS X apache install, edit /etc/apache2/httpd.conf with the directives found in the Django docs above. I can't speak for MAMP, but if you build Apache from source (which is so easy on OS X I do wonder why anyone bothers with MAMP... my insecurities are showing), edit /usr/local/apache2/conf/httpd.conf.

Jarret Hardie
Brilliant, I have mod_wsgi working and will as soon as I can work out the correct path for my app in the django.wsgi file I'll be away. Cheers!
Teifion
+3  A: 

Unless you are planning on going to production with OS X you might not want to bother. If you must do it, go straight to mod_wsgi. Don't bother with mod_python or older solutions. I did mod_python on Apache and while it runs great now, it took countless hours to set up.

Also, just to clarify something based on what you said: You're not going to find a mapping between the url path (like /polls) and a script that is being called. Django doesn't work like that. With Django your application is loaded into memory waiting for requests. Once a request comes in it gets dispatched through the url map that you created in urls.py. That boils down to a function call somewhere in your code.

That's why for a webserver like Apache you need a module like mod_wsgi, which gives your app a spot in memory in which to live. Compare that with something like CGI where the webserver executes a specific script on demand at a location that is physically mapped between the url and the filesystem.

I hope that's helpful and not telling you something you already knew. :)

Matt Youell
+3  A: 

If you're planning on running it for production (or non-development) mode another option is to do away with Apache and go with something lightweight like nginx. But if you're doing development work, you'll want to stick with the built-in server and PyDev in Eclipse. It's so much easier to walk through the server code line-by-line in the Eclipse debugger.

Installation instructions for nginx/Django here: http://snippets.dzone.com/tag/nginx

Ramin
+2  A: 

Yet another option is to consider using a virtual machine for your development. You can install a full version of whatever OS your production server will be running - say, Debian - and run your Apache and DB in the VM.

You can connect to the virtual disk in the Finder, so you can still use TextMate (or whatever) on OSX to do your editing.

I've had good experiences doing this via VMWare Fusion.

Daniel Roseman