Version 24 (modified by ed+at+swindelles+dot+us, 18 years ago) ( diff )

--

Goals

Getting Django to run on Windows, with pages served by IIS, and a SQL Server backend. Assume Windows Server 2003, IIS 6 and SQL Server 2005 where no versions are specified. You should already have Windows, IIS and SQL installed and working before following this guide.

Note: This doesn't work with Django <=0.91 as the IIS extenstion is written to the APIs in Django >=0.92 [SF]

Steps

  • Install Python
  • Install PyISAPIe - an extension for IIS to connect Python with IIS
  • Install Django and connect Django to PyISAPIe
  • Install Python Win32 extensions, as they are required by...
  • ADODB-API - the Python / SQL Server driver
  • Configure Django to look at SQL Server
  • Test, celebrate

Install Python

A simple download and install for this bit, so open http://www.python.org/ and look for "Quick Links" -> "Windows Installer" in the menu on the left. Download and run it, following the wizard steps until it is complete.

Install PyISAPIe - an ISAPI extension for IIS

There is no installer for this, so there are quite a few steps, but they aren't very complicated.

Breifly, this is an IIS extension that loads the Python interpreter into memory while IIS is running, and uses it to serve page requests - it avoids the CGI overhead of restarting Python for every request. It does mean that some Django files will be cached and you wont see changes while testing your Django site until you restart the Python process. With IIS 6 you have to right-click on the application pool running your services and select 'recycle' for changes to take. Previous versions of IIS might need the whole IIS service to be restarted.

Now on with the setup: 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.

I will assume you've extracted it to c:\pyisapie, and are using c:\python24 as the folder where Python is installed, but please change these where necessary for your setup.

There is documentation in the readme files, but at the time of writing the install consists of:

Setting up Files

  • Copy c:\pyisapie\PyISAPIe.dll to c:\python24\
  • Edit the properties -> security settings on that file, add "Network Service" and tick "read" permission. (So IIS can use it).
  • Go to c:\pyisapie\source\PyISAPIe\Python\ and copy the entire Http folder to c:\python24\lib\site-packages. Note: the Http folder is case sensitive. Saving in 'http' (or any other variation) will not work [SF].

Setting up IIS

You don't have to create a virtual directory - you can do this with the root folder. If you do use a virtual directory, say /myfolder, then only urls starting with /myfolder will be handled by PyISAPIe. If you use the root folder, all URLs will be handled by PyISAPIe, and you risk anything else on the site being inaccessible afterwards if you're not careful.

  • Open the IIS Management Console, and create a new virtual directory, and allow executing ISAPI extensions when prompted by the wizard.

IIS 6

  • View the properties of the new folder and click on the "configuration" button (if it's greyed out, click 'create' first), then add a new wildcard extension (the lower box), locate the pyisapie.dll file and untick the "check that file exists" box.
  • In the IIS Manager, go to the "Web Service Extensions" section, and right click -> add new web service extension.
    • Give it a name (it doesn't matter what), add the pyisapie.dll fill as a required file and check the box to set the extension status to allowed.

IIS 5

  • IIS 5.x doesn't support wildcard application maps, at least not through the configuration interface. On IIS 5, the procedure is as follows:
    • Right Click on virtual directory and choose "properties"
    • Ensure "Execute Permissions" is set to "Scripts and Executables"
    • Click "Configuration" (opposite "Scripts and Executables")
    • On "App Mappings" tab, select "Add"
    • For Executable, browse to pyisapie.dll (needs full path if you enter manually)
    • For extension enter * (this will redirect all requests to pyisapi)
    • You should probably limit the verbs allowed; I left as all since it was for internal use.
    • Untick the "check that file exists" box.
    • 'OK' to close all the open dialogs.

That's it installed. In c:\pyisapie\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://site/Info.py to test it. It should work. If it doesn't, when you find out why, please come back and explain what happened and how you fixed it. ;)

  • I got an error "Import Error: couldn't locate module Http.Isapi"; I resolved it by adding IUSR_machine read and execute access to the directory Http in site-packages [rnm]

Django

Please note that as of March 7th, 2007, the below instructions will not work for the latest development versions of Django. In order for PyISAPIe to work with Django (and vice versa), you must be using Django v. 0.95. Versions 0.95.1 and any higher produce numerous errors that seem to be related to PyISAPIe's stalled development. Thankfully, you can still download version 0.95 from the Django download site.

If you want to keep up with the latest Django development version, 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 the folder, and choose "SVN Checkout". Give the URL of the repository as: http://code.djangoproject.com/svn/django/trunk/ and click OK.)

Otherwise, just download the latest Django release from the main site, and extract the archive to a folder, e.g. c:\python24\django\.

Installing Django

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, because I don't have clear steps. If you know any, please put them here. Without doing that you can still test the Django install, but will need a full path to django-admin.py:

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

c:\test> cd proj

c:\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 PyISAPIe

Next, you must follow the readme.txt in the PyISAPIe examples\django folder, basically copy two files into specific folders. Then edit them a bit in order to fit your needs.

  • Put Isapi.py in c:\python24\lib\site-packages\Http folder. According to the example given above, you'll make the following changes to it:
# Add these two lines, and python will import DJANGO_SETTINGS_MODULE with no problem
import sys
sys.path.append(r'c:\test')

# Indicate the settings module of your project
os.environ["DJANGO_SETTINGS_MODULE"] = "proj.settings"
  • Put pyisapie.py in c:\python24\lib\site-packages\Django-xyz-123.egg\django\core\handlers folder. But there's a bug in PyISAPIe v1.0.3 that prevent it from working (hopefully they'll fix it in the next release). What you have to do is to add a single line of code to the pyisapie.py file you just copied:
# ...some stuff ahead

class PyISAPIeRequest(http.HttpRequest):
    def __init__(This):
        # ADD THIS LINE!
        This.method = Env.REQUEST_METHOD

        # ...and don't need to change the rest

# some stuff behind...
  • It doesn't seem that IIS+Python+PyISAPIe support multiple Django sites.

Serving Django with IIS

Supposed that you have successfully settled everything above, and pointed the virtual directory /myfolder to c:\test folder, we'll now make a Hello World page in Django.

  • Create a file called helloworld.py in c:\test\proj folder with the following content:
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello world!")
  • Modify urls.py in c:\test\proj folder, so you get the url "dispatched":
from django.conf.urls.defaults import *

urlpatterns = patterns('',
    # Example:
    # (r'^newtest/', include('proj.apps.foo.urls.foo')),
    (r'^.*$', 'proj.helloworld.index'),

    # Uncomment this for admin:
#     (r'^admin/', include('django.contrib.admin.urls')),
)

You have everything you need to have Django running from IIS. Now visit http://site/myfolder/ to take a look. Notice that you don't need to manually start a Django server.

Known Issues

  • On IIS 5, you have to use the console command "iisreset" for code changes to take effect. Just restarting the website in IIS Management Console has no use. How about IIS 6? (20070304/henrik: - same for iis6 on win2k3)
  • I had to create a "media" virtual directory for the stylesheets, etc, at the root of my IIS directory tree (urgh) with read permissions to get the admin site to load styles. Looking at the source for the admin page in my browser, the stylesheet link is absolute (/media/...) [rnm]

Install the PyWin32 Extensions

Just a download and install for this bit - Pick them up from http://sourceforge.net/project/showfiles.php?group_id=78018

Install the ADO-DB-API SQL Server interface

Go to the homepage, which is http://adodbapi.sourceforge.net/ then follow the link to download the latest version.

Then extract the zip file somewhere, e.g. c:\python24\adodbapi, and run 'setup.bat' from that folder.

Configure Django to look at SQL Server

At the moment (21st Sept 2006), the Django database backend ado_mssql from SVN trunk isn't fully working. I think the patch 'diff3' attached to this Trac entry looks promising: http://code.djangoproject.com/ticket/2358

Credits

Note: See TracWiki for help on using the wiki.
Back to Top