| 1 |
================================= |
|---|
| 2 |
How to use Django as a CGI script |
|---|
| 3 |
================================= |
|---|
| 4 |
|
|---|
| 5 |
Although the `current preferred setup`_ for running Django is Apache_ with |
|---|
| 6 |
`mod_python`_, many people use shared hosting, on which CGI is the only |
|---|
| 7 |
viable option. |
|---|
| 8 |
|
|---|
| 9 |
Note that the CGI mode is the lowest method of serving dynamic web pages, if |
|---|
| 10 |
you have the option, go for the `mod_python method`_ or the `FastCGI method`_. |
|---|
| 11 |
You should only use this method as a last resort. |
|---|
| 12 |
|
|---|
| 13 |
.. _mod_python method: ../modpython/ |
|---|
| 14 |
.. _FastCGI method: ../fastcgi/ |
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
Creating your cgi script |
|---|
| 18 |
======================== |
|---|
| 19 |
|
|---|
| 20 |
For this method to work you will need a wrapper script inside your cgi-bin |
|---|
| 21 |
directory, the job of this script is to set the environment for Django to run |
|---|
| 22 |
and then call the cgi runner. |
|---|
| 23 |
|
|---|
| 24 |
You can use this as an example: |
|---|
| 25 |
|
|---|
| 26 |
#!/usr/bin/python |
|---|
| 27 |
import sys, os |
|---|
| 28 |
|
|---|
| 29 |
# Add a custom Python path, you'll want to add the parent folder of |
|---|
| 30 |
# your project directory. |
|---|
| 31 |
sys.path.insert(0, "/home/user/django/") |
|---|
| 32 |
|
|---|
| 33 |
# Switch to the directory of your project. (Optional.) |
|---|
| 34 |
# os.chdir("/home/user/django/myproject/") |
|---|
| 35 |
|
|---|
| 36 |
# Set the DJANGO_SETTINGS_MODULE environment variable. |
|---|
| 37 |
os.environ['DJANGO_SETTINGS_MODULE'] = "myproject.settings" |
|---|
| 38 |
|
|---|
| 39 |
from django.core.servers.cgi import runcgi |
|---|
| 40 |
runcgi() |
|---|