Code


Version 6 (modified by phillip@…, 8 years ago) (diff)

Just some notes

Goals

The aim of this page is to describe getting Django to run on Windows, served by IIS and with an SQL Server backend.

Useful for people who already have such a setup (You wont find me discussing the installation of SQL Server here) and can't justify (or just don't want) Apache and PostgreSQL as well.

This may take a while. Best attempted with a mug of caffeinated beverage handy.

NB, I'm working from Windows Server 2003/IIS 6 and SQL Server 2005.

NB also: The whole point of this is to keep Python running even when not serving requests, to reduce the overhead of starting Python all the time. This means that a lot of changes you make while configuring it wont take effect until you do a full IIS restart.

Note: With IIS 6, you only have to right-click on the application pool running your services and select 'recycle' for changes to take.

Credit to: http://thinkhole.org/wp/2006/04/03/django-on-windows-howto/ for the Django on Windows with Postgres/Apache guide, from which I have grabbed some parts of the django-on-windows setup.

Steps:

  • Install Python
  • Install Python's Windows extensions
  • Install an ISAPI extension for IIS
  • Install Django
  • Combine Django and IIS
  • Configure Django to look at SQL Server
  • Test
  • Celebrate

Install Python

Head to http://www.python.org/ and look at the menu on the left; in the "quick links" section is a link to the Windows installer for the latest stable version of Python. Download and run it, following the wizard steps until it is complete.

Phew. Easy bit over with, it gets harder from here on in.

Install Win32 extensions for Python

These are some Windows specific extensions for Python, and they are (I think) required for the IIS driver later.

Visit https://sourceforge.net/projects/pywin32/ and download the Windows installer, run through it. ( That URL looks neater, but the download link gives Error 500 at the moment. This one seems to work though: http://sourceforge.net/project/showfiles.php?group_id=78018 )

Install an ISAPI extension for IIS

Lots of steps, and no installer.

This is an extension that loads the Python interpreter into memory while IIS is running, and uses it to serve requests more quickly. CGI apps need to start a new instance of the interpreter for every request, this does not have that overhead - it performs a similar service to mod_python on Apache servers.

Go to http://pyisapie.sourceforge.net/ and download it (look in the menu on the right, under "Links" for a download link). Unzip the archive so you can copy files out of it.

Documentation is in the readme files, but at the time of writing the install is:

  • Copy the PyISAPIe.dll file to your Python path, e.g. c:\python24
  • Set the security permissions on the dll file so that IIS can read it. In the case of IIS/Server 2003 you need to give read permissions to the "Network Service" account
  • Look in the <extracted archive>\source\PyISAPIe\Python\ folder and copy the Http folder to c:\python24\lib\site-packages (adjust, depending on where you installed Python)
  • Open the IIS Manager from Administrative Tools, and create a new virtual directory in your website (give it permissions to execute ISAPI extensions when the wizard asks.
  • Go to the properties of the folder, click on the configuration button (if it's greyed out, click 'create' first), and add a new wildcard extension (the lower box), locate the pyisapie.dll file and untick the "check file exists" box.
  • IIS 6 Specific: In the IIS Manager, go to the "Web Service Extensions" section, and right click -> add new web service extension.
    • Give it a name, add the pyisapie.dll fill as a required file and check the box to set the extension status to allowed.

That's it installed. In the <extracted archive>\source\PyISAPIe\Python\examples folder is a file called Info.py. Copy this to your new virtual directory folder, and then try and view http:// your site / Info.py

Notes:

  • You don't _have_ to create a virtual directory - you can do this with the root folder. If you do choose a virtual directory, say /myfolder, then only urls starting with /myfolder will be handled by PyISAPIe.
  • IIS 5.x doesn't support wildcard application maps, at least not through the configuration interface.

It should work. If it doesn't, when you find out why please come back and explain what happened and how you fixed it. ;)

The PyISAPIe extension has a driver for Django as well, so that's where I will be heading, after this:

Installing Django

I'm not going to say what this is; if you're here, following this, then you should really know by now!

Get the development version if you wish

If you want to keep up with the latest Django developments, you will need to download and install a subversion client for Windows. (e.g. Tortoise SVN, install it, then create a new folder somewhere. Right click on it, and choose to SVN Checkout. Give the URL of the repository as: http://code.djangoproject.com/svn/django/trunk/ and click OK.)

Or get the normal version

Otherwise, just download Django from the main site, and extract the archive to a folder.

Do The Install

Drop to a command prompt (start -> run -> cmd -> OK), change to your Python folder and install Django. Example:

c:\> cd python24\django
c:\python24\django> python setup.py install

NB: You will probably need to do some messing around with paths and such to make this work neatly. I'm afraid I'm leaving that to you for now. Without doing that you can still test the Django install, but will need a full path to django-admin.py:

E:\> md test
e:\> cd test
E:\test>d:\Python24\django\django\bin\django-admin.py startproject proj

E:\test>cd proj

E:\test\proj>python manage.py runserver
Validating models...
0 errors found.

Django version 0.95 (post-magic-removal), using settings 'proj.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Now open a web browser, and visit the site. It should serve a neat page to you.

Linking Django to PyISAPI

Next, you must follow the readme in the PyISAPI examples\django folder (it explains where to copy two files - one goes into the Django install (One goes into python24\lib\site-packages\django-xyz-123.egg\core\handlers\). Then edit the isapi.py file you just copied to lib\site-packages\http\. See where it says you should change a line? If applicable, edit it for your django.settings.module settings.

Serving Django with IIS

You have created a new virtual folder in IIS, and added the PyISAPI extension and tweaked it to drive django sites. There is also an addition to Django so it can be driven by PyISAPIe. And, you can now use django-admin.py to create a site inside the virtual folder.

You have everything you need to have Django running from IIS.

... I'll leave you to sort out your Import Path and DJANGO_SETTINGS_MODULE and stuff ...

Now, while I don't have an app running, I do have Django errors rather than IIS errors. I need to get my model access sorted for my test app.

Configure Django to look at SQL Server

With any luck, the suggestion that there is an ado_mssql backend will do, and this will be easy.