Friday, January 20, 2012

Basic Django on Apache2

My Django project is called 'automation' and lives in the directory /home/tlichtenberg/workspace/ptest/django/src/automation

It has CSS files in the regression/static/styles directory under the home project. I needed to add some stuff to Apache to allow it to find the files it needs to run the Django project

--------------------------------

My Apache2 httpd.conf:


WSGIScriptAlias / /home/tlichtenberg/workspace/ptest/django/src/automation/django.wsgi

AliasMatch ^/([^/]*\.css) /home/tlichtenberg/workspace/ptest/django/src/automation/regression/static/styles/$1

Alias /static/ /home/tlichtenberg/workspace/ptest/django/src/automation/regression/static/

<Directory /home/tlichtenberg/workspace/ptest/django/src/automation/regression/static>
Order deny,allow
Allow from all
</Directory>

<Directory /home/tlichtenberg/workspace/ptest/django/src/automation>
Order deny,allow
Allow from all
</Directory>

------------------------------

The django,wsgi file is critical for setting the path and connecting Django to Apache. It looks like this:


import os
import sys

path = '/home/tlichtenberg/workspace/ptest/django/src/automation/'
if path not in sys.path:
   sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

------------------------------------------

The 'settings' defined by django,wsgi points to the Django project's 'settings.py' file, which defines a lot of the paths that Django needs. Also important is the 'urls.py' file, like settings.py, it is a standard for all Django projects and defines all the URLs associated with the web applications being served by Django. For example:

settings.py:

  STATIC_ROOT = '/home/tlichtenberg/workspace/ptest/django/src/automation/regression/static/'
  STATIC_URL = '/static/'
  ROOT_URLCONF = 'urls'
  INSTALLED_APPS = (
    'my_project',
    'my_other_project',
 )

urls.py
   (r'^my_project/$', 'my_project.views.my_project_main'),
defines that any url beginning with 'my_project' (e.g. http://localhost/my_project) will direct the code to the my_project_main method in the project's views.py file.

---------------------------------------------------


 


No comments:

Post a Comment