Ticket #2407: cgi.txt

File cgi.txt, 1.3 KB (added by Marc Fargas <telenieko@…>, 8 years ago)

to be placed in docs/

Line 
1=================================
2How to use Django as a CGI script
3=================================
4
5Although the `current preferred setup`_ for running Django is Apache_ with
6`mod_python`_, many people use shared hosting, on which CGI is the only
7viable option.
8
9Note that the CGI mode is the lowest method of serving dynamic web pages, if
10you have the option, go for the `mod_python method`_ or the `FastCGI method`_.
11You should only use this method as a last resort.
12
13.. _mod_python method: ../modpython/
14.. _FastCGI method: ../fastcgi/
15
16
17Creating your cgi script
18========================
19
20For this method to work you will need a wrapper script inside your cgi-bin
21directory, the job of this script is to set the environment for Django to run
22and then call the cgi runner.
23
24You 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()
Back to Top