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 views.py 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 settings.py 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 settings.py
file:

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 urls.py:

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

Finally, to view the messages, pointer your browser to
/debug_messages/

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=file(get_filename(),'a+')
    fp.write(str(datetime.now())+':  '+message)
    fp.close()

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

def clear():
    fname=get_filename()
    if os.path.exists(fname):
        os.remove(fname)

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

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

    return render_to_response("debug_messages.html",\
                              {'messages':messages,
                               'site_url':settings.SITE_URL})

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:

<html>
<head>
<title>Debug Messages</title>
</head>

<body>
<h1>Debug Messages</h1>

{% for x in messages %}
<p>{{x}}<br><hr></p>
{% endfor %}

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

</body>
</html>

Next, add a new variable to your settings.py 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 urls.py 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:

root/debug_messages/

Happy debugging!

Last modified 17 years ago Last modified on Dec 21, 2006, 7:05:54 PM

Attachments (1)

Download all attachments as: .zip

Note: See TracWiki for help on using the wiki.
Back to Top