Django

Installation

  1. Open Web Platform Installer and install "Python project":
  2. Follow steps in "Creating Django project":


Deployment

To deploy Python Django application you will need to install Python Hosting Package on a target server. Then just copy IIS web site from one machine to another.

web.config example

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 <system.webServer>
  <heliconZoo>
   <application name="python.project" >
    <environmentVariables>
     <add name="VIRTUAL_ENV" value="%APPL_PHYSICAL_PATH%\venv" />
     <add name="PATH" value="%APPL_PHYSICAL_PATH%\venv\Scripts;%PATH%" />
     <add name="PYTHONPATH" value="%APPL_PHYSICAL_PATH%\venv\lib\site-packages;
     %APPL_PHYSICAL_PATH%\venv\lib;%APPL_PHYSICAL_PATH%;%APPL_PHYSICAL_PATH%\project" />

     <!-- generic wsgi app -->
     <!-- <add name="WSGI_APP" value="welcome.application" />  -->

     <!-- django project -->
     <add name="DJANGO_SETTINGS_MODULE" value="project.settings" />

     <!-- tornado app -->
     <!-- <add name="APP_WORKER" value="%APPL_PHYSICAL_PATH%\server.py" /> -->

     <!-- web console -->
     <!-- security rules for console are placed in /console/web.config -->
     <add name="CONSOLE_URL" value="console" />

     <!-- application deployment -->
     <add name="DEPLOY_FILE" value="deploy.py" />
     <!-- <add name="DEPLOY_LOG" value="deploy.log" /> -->

     <!-- logging -->
     <add name="ERROR_LOG_DIR"  value="log" />

     <!-- reload on files changes -->
     <!-- <add name="WATCH_FILE_CHANGES_MASK" value="*.py" /> -->

    </environmentVariables>
   </application>
  </heliconZoo>

  <handlers>

   <!-- Django or WSGI application over fastcgi -->
   <add name="python.project#x86" scriptProcessor="python.2.7.wsgi" path="*" verb="*"
   modules="HeliconZoo_x86" preCondition="bitness32" resourceType="Unspecified" requireAccess="Script" />
   <add name="python.project#x64" scriptProcessor="python.2.7.wsgi" path="*" verb="*"
   modules="HeliconZoo_x64" preCondition="bitness64" resourceType="Unspecified" requireAccess="Script" />

   <!-- HTTP backend (Tornado) over http -->
   <!--
   <add name="python.project#x86" scriptProcessor="python.2.7.http" path="*" verb="*"
   modules="HeliconZoo_x86" preCondition="bitness32" resourceType="Unspecified" requireAccess="Script" />
   <add name="python.project#x64" scriptProcessor="python.2.7.http" path="*" verb="*"
   modules="HeliconZoo_x64" preCondition="bitness64" resourceType="Unspecified" requireAccess="Script" />
    -->

  </handlers>

 </system.webServer>
</configuration>

Environment variables

  • PYTHONPATH — optional path to Python modules. In the example it points to application root, the python_modules\Lib\site-packages subfolder and also includes value defined by user.
  • DJANGO_SETTINGS_MODULE — optional path to a file with Django application settings. By default it’s settings.py.
  • django.root — optional virtual path of the application. In the example its value is taken from the %APPL_VIRTUAL_PATH% variable.
  • DEPLOY_FILE — optional path to a a script which is run before application startup and every time IIS application pool recycles. The script usually contains deployment instructions to satisfy application dependencies and database migrations.
  • DEPLOY_LOG — optional path to a text file containing the output of the deploy script. It’s recommended to set this variable if you use deploy script.
  • WATCH_FILE_CHANGES_MASK — files mask to watch for chnages. Worker is restarted every time a file of specified type changes. In the example all .py files are monitored.

web.config to disable Django in directory with media/static files:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <!-- 
      This removes Helicon Zoo handler and makes IIS processing static files.
      -->
      <remove name="django.project#x64" />
      <remove name="django.project#x86" />
    </handlers>
  </system.webServer>
</configuration>

deploy.py file example:

Deploy file is used to execute commands, such as components installations or database migrations, on a server when the application initially starts or updated. The following code is only an example. We recommend you to use deploy.py file that comes with the Python project template.

# This file is example of deploy.py
#
# Path to this file is configured in web.config:
#      <application name="django.project.x86" >
#        <environmentVariables>
#          <add name="DEPLOY_FILE" value="deploy.py" />
#          <add name="DEPLOY_LOG"  value="log\deploy.log" />
#        </environmentVariables>
#      </application>
#
# The file is executed once on the first request after every restart of IIS application.
# The file output is redirected to log file described in DEPLOG_LOG environment variable.
#

import sys
import os
import os.path


VIRTUALENV_EXE = os.path.join(os.path.dirname(sys.executable), 'scripts\\virtualenv.exe')
VIRTUALENV_NAME = 'venv'
DJANGO_PROJECT_NAME = 'project'
PROJECT_DIR = os.path.dirname(__file__)


def run(command, exit_on_error=True):
    print('\nRunning command: '+command)
    status = os.system(command)
    if status != 0:
        sys.exit(status)


def django_manage(command):
    run(DJANGO_PROJECT_NAME + '\\manage.py ' + command)


def main():
    os.chdir(PROJECT_DIR)

    # update APPDATA env for pip
    #os.environ['APPDATA'] = os.path.join(PROJECT_DIR, PYTHON_MODULES_DIR)

    # create virtual environment
    if not os.path.exists(VIRTUALENV_NAME):
        run(VIRTUALENV_EXE+' '+VIRTUALENV_NAME)

    # install requirements to local folder
    run('pip install --requirement=requirements.txt')

    # run manage.py syncdb --noinput
    #django_manage('syncdb --noinput')

    # run manage.py migrate
    #django_manage('migrate')

    # collect static
    #django_manage('collectstatic --noinput')

    # that's all
    print "Bye!"


if __name__=='__main__':
    main()