One More Blog

Welcome to the Blog of Eric Moritz; wannabe philathropist, geek and over-all swell guy

Django - Python on the web matures

For a long time people have been wanting to use Python in web development. Today is the day that Python will rival Ruby on Rails for it's throne. Today was the day that Django was reveiled in beta form. It looks promising.

I have been working with the folks on the project testing out their installation scripts and I must say that this thing is great. I've never seen something so easy. Even in it's infancy Django is mature. The only thing that they are working on is making the installation and configuration easier. By the end of today I'm sure they'll have everything documented and everyone will be able to play.

For now I've managed to figure out what exactly gets Django and Apache to work together. Please keep in mind that this is how I got it to work. My experience with mod_python is not that great so bare with me.

First I'd like to point out that I'm working from the point after their first tutorial. I'm using the same exact classes that they are using. Complete that tutorial and then you can follow along with me. After that, you are on your own.

After the tutorial I wanted to get the system talking to the web, so what I did was create a folder in my webroot (/var/www/html) called /var/www/html/dj this acts as a stub for the django system. There isn't any files in the folder except my .htaccess file that tells apache to call up Django when someone accesses this folder from a browser. You can very well configure mod_python for your whole server but I have so many other things in subfolders of my server that I went this route. So I'll start with how I configured Apache and what is in my .htaccess file, if you want to do it another way, you'll have to figure it out yourself. I hope this will help nonetheless.

Apache Config


# For dev purposes, this makes apache reload mod_python for each request, good for development, bad for production
KeepAlive off
MaxRequestsPerChild 1

#Load tho module
LoadModule python_module modules/mod_python.so

# Tell apache to allow my dj folder to use .htaccess
<directory "/var/www/html/dj">
AllowOverride FileInfo
</directory>

.htaccess


SetHandler python-program
PythonHandler django.core.handler
SetEnv DJANGO_SETTINGS_MODULE myproject.settings.main
PythonPath sys.path+['/var/www/pyapps']
PythonDebug On

I have to point out one thing. When my created the myproject Django project, we put it in a folder. On my system, I put it in /var/www/pyapps You will have to change the path to were you put you myproject.

Now I thought, lets set up the url mappings like they say in all the docs on their site. I edited the file: myproject/apps/polls/urls/polls.py and made it look like this:

from django.conf.urls.defaults import *
urlpatterns = patterns('myproject.apps.polls.views.polls',
(r'polls/questions/(?P<question_id>\d*)/$', 'view_one'),
)

I pointed my browser to http://localhost/dj/polls/questions/1/ and got:

Mod_python error: "PythonHandler django.core.handler"

Traceback (most recent call last):

File "/usr/lib/python2.4/site-packages/mod_python/apache.py", line 299, in HandlerDispatch
result = object(req)

File "/usr/lib/python2.4/site-packages/django/core/handler.py", line 157, in handler
return CoreHandler()(req)

File "/usr/lib/python2.4/site-packages/django/core/handler.py", line 31, in __call__
response = self.get_response(req.uri, request)

File "/usr/lib/python2.4/site-packages/django/core/handler.py", line 87, in get_response
from django.conf.settings import DEBUG, INTERNAL_IPS, ROOT_URLCONF

ImportError: cannot import name ROOT_URLCONF

What the hell is ROOT_URLCONF? After investigation (i.e. looking at http://code.djangoproject.com/svn/djangoproject.com/django_website/settings/main.py),
ROOT_URLCONF is the main module used to get for the urlpatterns variable.

So I copied the pattern that they have for their settings.urls module in http://code.djangoproject.com/svn/djangoproject.com/django_website/settings/

For myproject.settings.urls.main I now have this:

myproject.settings.urls.main


from django.conf.urls.defaults import *

urlpatterns = patterns('',
(r'polls/', include('myproject.apps.polls.urls.polls')),
)

and for myproject.apps.polls.urls.polls


from django.conf.urls.defaults import *

urlpatterns = patterns('myproject.apps.polls.views.polls',
(r'questions/(?P<question_id>\d*)/$', 'view_one'),
(r'questions/(?P<question_id>\d*)/vote/$', 'vote'),
)

I then created a view in myproject.apps.polls.views.polls

myproject.apps.polls.views.polls


try:
from django.models.polls import polls, choices
from django.utils.httpwrappers import HttpResponse,HttpResponseRedirect
from django.core import template_loader
from django.core.template import *
except Exception, e:
raise str(e)

def view_one(request,question_id):
try:
p = polls.get_object(id__exact=question_id)
except PollDoesNotExist:
raise Http404

t = template_loader.get_template('polls/question_detail')
c = Context({'poll' : p})
content = t.render(c)
return HttpResponse(content)

def vote(request,question_id):
try:
p = polls.get_object(id__exact=question_id)
except PollDoesNotExist:
raise Http404

if(request["choice"]):
choice_id = request["choice"]
c = choices.get_object(id__exact=choice_id)

c.votes = c.votes + 1

c.save()
return HttpResponse(str(c) + ' has ' + str(c.votes) + ' votes')

For my templates, I have two:

  1. question_display.html
  2. base.html

Place those in a folder and set TEMPLATE_DIRS myproject.settings.main, mine looks like


TEMPLATE_DIRS = (
# Put strings here, like '/home/html/django_templates'.
'/var/www/html/dj/templates',
)

Posted by on July 17, 2005

View Comments

Tags: django, news

blog comments powered by Disqus
Who are you? Log In | Sign up