Publish a Django application in IIS on Windows

Source: Internet
Author: User
Tags install django
ArticleDirectory
    • Install pyisapie-an iis isapi extension
    • Connect Django to pyisapie
    • Modify Django settings
    • Original document reference
Target

Deploy Applications created by Django on the IIS server on Windows. Before starting, make sure that you have installed Windows and IIS and can run properly. The Django application to be released has been debugged on the built-in test server.

Note: This guide is invalid for Django <= 0.91, because IIS extensions are based on APIs of Django> = 0.92.

Procedure
    • Install pyisapie-an extension that allows IIS to support Python
    • Install Django and connect to Django through pyisapie
    • Configure Django
    • Test
Install pyisapie-an iis isapi extension

Pyisapie is not released as an installation package, but it is not complicated to install

Simply put, this extension loads the python interpreter into the memory during IIS runtime and uses it to respond to page requests. It avoids repeatedly starting python for each request in CGI Mode. This means that some Django files will be cached.CodeIt will not take effect immediately until you restart the python process. For IIS 6, you must right-click the applicationProgramSelect "refresh" to make the change effective. For earlier IIS versions, you may need to restart the entire IIS service.

Well, visit http://pyisapie.sourceforge.net/and download the latest pyisapie.

Assume that you decompress all the files to c: \ pyisapie and install python in c: \ python24. If the path is different, make appropriate adjustments.

The steps in readme are described as follows:

Installation File
    • Copy c: \ pyisapie. DLL to c: \ python24 \
    • Edit "properties"-> "Security Settings" of the file, add "network" and "service", and select "read. (To make IIS accessible)
    • Go to c: \ pyisapie \ source \ pyisapie \ Python \ and copy the entire HTTP folder to C: \ python24 \ Lib \ Site-packages.Note: The name of an HTTP folder is case sensitive. Do not mistakenly save it as HTTP or another variant, and the folder will not run properly.
Set IIS

You do not have to create a virtual directory-you can directly use the root directory. If you use a virtual directory, such as/myfolder, only the pages starting with/myfolder are processed by pyisapie. If you use the root directory, all URLs will be processed by pyisapie, which may cause other contents on your site to be inaccessible. * Open the IIS console, create a new virtual directory, and allow ISAPI extension when prompted by the wizard.

IIS 6
    • View the properties of the virtual directory and click "Configure" (If this option is gray, click "CREATE" first), and then add a new wildcard (in the list box below ), go to pyisapie. DLL file to clear the "check whether the file exists" option.
    • In IIS manager, right-click "network service expansion" and choose "Add new network service extension.
    • Add pyisapie. dll as the required file and select allow extension.
IIS 5
    • IIS 5.x does not support wildcard application ing, at least not on the configuration page. In IIS 5, the implementation steps are as follows:
    • Right-click the virtual directory and select "properties"
    • Make sure that the "execution license" is set to "script and executable program"
    • Click "Configure" (next to "script and executable program)
    • Click the "Application ing" tab and select "add"
    • Set "executable file" to c: \ python24 \ pyisapie. dll
    • Set "extension" to "*" so that all requests are processed by pyisapie. dll.
    • You should limit the permitted actions, but for the convenience of internal testing, we will temporarily set it to "all actions"
    • Clear the "check whether the file exists" option
    • Click "OK" to close all windows

After setting, you can set the info under c: \ pyisapie \ source \ pyisapie \ Python \ examples. copy py to your new virtual directory, and then access http: // site/info. PY to test.

Connect Django to pyisapie

Set Django according to readme.txt In the examples \ djangodirectory of pyisapie. In short, two files are copied to the corresponding folder. Then, make a slight modification to meet your needs.

    • Put ISAPI. py in the C: \ python24 \ Lib \ Site-packages \ HTTP directory. As shown in the preceding example, You Need To slightly modify the file:

Modified code

# Add the following two lines so that python can import django_settings_moduleimport syssys. path. append (r 'C: \ test') # indicate the settings module of your projectos. environ ["django_settings_module"] = "proj. settings"
    • Put pyisapie. py in the C: \ python24 \ Lib \ Site-packages \ Django-xyz-123.egg \ Django \ core \ handlers directory. Pyisapie v1.0.3 has a small bug. You need to modify it a little before it can be used normally.

Modified code

 
Class pyisapierequest (HTTP. httprequest): def _ init _ (this): # add this. method = env. request_method to the next line. # do not modify other attributes.
    • IIS + Python + pyisapie does not currently support multiple Django sites.
Modify Django settings

After completing the preceding steps, we can point the virtual directory/myfolder to c: \ test and create a hello World page for testing.

    • Create a helloworld. py file in the C: \ test \ proj directory.

The content is as follows:

 
From Django. Http import httpresponsedef index (request): Return httpresponse ("Hello world! ")
    • Modify URLs. py in the C: \ test \ proj directory

The content is as follows:

 
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 ')),)

Everything is ready. You can visit http: // site/myfolder immediately to check the effect. Note that you do not need to manually start the Django service.

FAQs
    • On IIS 5, after each code modification, You need to restart IIS on the console with the command "iisreset" to make it take effect. It is ineffective to restart the site directly on the "IIS console". You must restart the entire IIS service.

    • It is best to create a "Media" virtual directory under the root directory to provide static content such as style sheets.

Original document reference
    • Djangoonwindowswithiisandsqlserver

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.