Sometimes a strategically placed print statement can be invaluable in solving a programming problem. In the development environment (in windows), print statements just print to the console. But what do you do if your code won’t work when it’s run from the server?

Below, I describe a simple app that writes those messages to a file and then displays those messages in a webpage. To start out, create a new app in your project. Adjust the permissions on the app folder so that your view can write to the folder. Next, paste the following code into the file in your app.

Functions for reading, writing and displaying debugging messages.

This program writes debugging messages to a text file. In order to
do this, the program must know where to write this file and have write
privelges to the folder that will contain this file.

To accomplish this, put a variable in your folder called 
DEBUG_MESSAGES_ROOT. Set this variable to the path to the folder where
you want to write the message file. This is what I have in my

DEBUG_MESSAGES_ROOT=r'C:\Documents and Settings\CCM\Desktop\pldev\debug_messages'

Note the last item is a folder name and there is no trailing separator.

To write debugging messages, in the file where you want to create
messages put:

import debug_messages.views as dbm

To post a message call the function:

dbm.write(your message)

To enable viewing of the files, add to your

(r'^debug_messages/$', 'yourproject.debug_messages.views.index')

Finally, to view the messages, pointer your browser to

You can clear the file by using the "Clear" button on this web page, or
by deleting the file.
from django.shortcuts import render_to_response
from django.conf import settings
import os
import os.path
from datetime import datetime

# Functions for managing the message file. "write" is the only function
# you will need to directly access.

def write(message):
    fp.write(str(':  '+message)

def get_filename():
    return os.path.join(settings.DEBUG_MESSAGES_ROOT,'messages.txt')

def clear():
    if os.path.exists(fname):

def read():
    if not os.path.exists(fname):
        return ['No Messages.']
    return the_messages
# The view for displaying the messages and clearing the message file.
def index(request):

    if request.POST.get('clear','')=='clear':
        messages=['No Messages.']

    return render_to_response("debug_messages.html",\

Create a subfolder in your app called templates. In that folder create a file called "debug_messages.html" and paste the following code into it:

<title>Debug Messages</title>

<h1>Debug Messages</h1>

{% for x in messages %}
{% endfor %}

<form method="post" action="{{site_url}}/debug_messages/" >
<input type="hidden" name="clear" value="clear" />
<input type="submit" value="Clear" />


Next, add a new variable to your file. Call it DEBUG_MESSAGES_ROOT and set it equal to the path to the folder containing this app.

To enable the url for viewing the messages add the following to your file in your project (not this app):

(r'^debug_messages/$', 'yourproject.debug_messages.views.index')

To write debugging messages from one of your python scripts, add this import statement to that script:

import debug_messages.views as dbm

Next, in your script add lines to print messages of the form:

dbm.write(your message)

"Your message" should be a string. If you want each message to start on a newline, then make sure the string ends it a \n.

To view the messages, point your browswer to the url:


Happy debugging!

Last modified 16 years ago Last modified on 12/22/06 01:05:54

Attachments (1)

Download all attachments as: .zip

Back to Top