================================= How to use Django as a CGI script ================================= Although the `current preferred setup`_ for running Django is Apache_ with `mod_python`_, many people use shared hosting, on which CGI is the only viable option. Note that the CGI mode is the lowest method of serving dynamic web pages, if you have the option, go for the `mod_python method`_ or the `FastCGI method`_. You should only use this method as a last resort. .. _mod_python method: ../modpython/ .. _FastCGI method: ../fastcgi/ Creating your cgi script ======================== For this method to work you will need a wrapper script inside your cgi-bin directory, the job of this script is to set the environment for Django to run and then call the cgi runner. You can use this as an example: #!/usr/bin/python import sys, os # Add a custom Python path, you'll want to add the parent folder of # your project directory. sys.path.insert(0, "/home/user/django/") # Switch to the directory of your project. (Optional.) # os.chdir("/home/user/django/myproject/") # Set the DJANGO_SETTINGS_MODULE environment variable. os.environ['DJANGO_SETTINGS_MODULE'] = "myproject.settings" from django.core.servers.cgi import runcgi runcgi()