Django Quickstart
From Apis Networks Wiki
Contents |
Overview
Django is a Python-based Web framework specializing in rapid development. Design ideology is similar to that of the Rails framework for Ruby.
Django deployment requires the Basic package or higher, which features shell access. Django should run behind a FastCGI process for best performance.
Our stock Python version is limited to 2.3 due to underlying dependencies. You may build your own Python distribution, but be sure to change the shebang from
#!/usr/bin/python to #!/usr/local/bin/python in the example code.Installing Django
1. Login to your shell as the main account holder.
2. Download and decompress the latest stable Django release. At the time of the guide this version was 0.96. After decompressing the distribution, change into the directory (Django-0.96/ in this case) and install. wget -O django.tar.gz http://www.djangoproject.com/download/0.96/tarball/ tar -xvzf django.tar.gz rm -f django.tar.gz cd Django-0.96 python setup.py install --prefix=/usr/local/3. Install flup, which is a Python WSGI handler for FastCGI. flup will automatically install setuptools and inform you with a 15 second prompt. Let the message idle out and setup will continue.
wget -O flup.tar.gz http://www.saddi.com/software/flup/dist/flup-0.5.tar.gz tar -xvzf flup.tar.gz rm -f flup.tar.gz cd flup-0.5 python setup.py install --prefix=/usr/local/
Initializing a Project
For our example project, the application codebase named myapp will reside under /home/msaladna/. myapp will be accessible via http://django.mydomain.com whose document root resides under /var/www/django/.
cd /home/msaladna/
django-admin.py startproject myapp
2. Create a simple Python script named index.fcgi under /var/www/django/. #!/usr/bin/python import sys, os # Add the application's parent directory sys.path.append("/home/msaladna") # Set the DJANGO_SETTINGS_MODULE environment variable. os.environ['DJANGO_SETTINGS_MODULE'] = "myapp.settings" from django.core.servers.fastcgi import runfastcgi runfastcgi(method="threaded", daemonize="false")3. To ensure your application runs as a CGI application, permissions must be set to
755 for both the parent directory and index.fcgi file. chmod 755 /var/www/django/ /var/www/django/index.fcgi4. All requests must be transparently passed to the FastCGI handler. This may be accomplished with two simple rewrite rules in the .htaccess file:
RewriteEngine On RewriteBase / RewriteRule ^$ index.fcgi/ [QSA,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.fcgi/$1 [QSA,L]5. Access the URL in your browser and you should see Welcome to Django. You are ready to follow along Django's tutorial and setup the database. If at any time you need to force FastCGI to reload the code, updating the
index.fcgi modification timestamp with the touch command will work: touch index.fcgi A subsequent access to Django should be noticeably slower than before. The delay is caused by FastCGI reloading the application with the new code.
Python 2.5 Modules
If you build a new version of Python you may need to build a module such as a database connector. In this example you will see how to install the current version of MySQLdb, also know as MySQL-Python. Here are the steps you need to take:wget http://superb-east.dl.sourceforge.net/sourceforge/mysql-python/MySQL-python-1.2.2.tar.gz tar -xvzf MySQL-python-1.2.2.tar.gz rm -f MySQL-python-1.2.2.tar.gz cd MySQL-python-1.2.2/ python setup.py install --prefix=/usr/local/You should be all set to the MySQLdb module in your Python code. To test the module availability, open up a Python shell and try to import the module:
If you don't see any errors your are ready to use the module.Python 2.5.2 (r252:60911, Nov 21 2008, 17:44:22) [GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>>
