Changes between Initial Version and Version 1 of SimplePrintAppForDebugging


Ignore:
Timestamp:
Dec 21, 2006, 6:49:40 PM (18 years ago)
Author:
Chuck Martin <cwurld@…>
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • SimplePrintAppForDebugging

    v1 v1  
     1Sometimes 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?
     2
     3Below, 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.
     4
     5
     6{{{
     7'''
     8Functions for reading, writing and displaying debugging messages.
     9
     10This program writes debugging messages to a text file. In order to
     11do this, the program must know where to write this file and have write
     12privelges to the folder that will contain this file.
     13
     14To accomplish this, put a variable in your settings.py folder called
     15DEBUG_MESSAGES_ROOT. Set this variable to the path to the folder where
     16you want to write the message file. This is what I have in my settings.py
     17file:
     18
     19DEBUG_MESSAGES_ROOT=r'C:\Documents and Settings\CCM\Desktop\pldev\debug_messages'
     20
     21Note the last item is a folder name and there is no trailing separator.
     22
     23To write debugging messages, in the file where you want to create
     24messages put:
     25
     26import debug_messages.views as dbm
     27
     28To post a message call the function:
     29
     30dbm.write(your message)
     31
     32To enable viewing of the files, add to your urls.py:
     33
     34(r'^debug_messages/$', 'yourproject.debug_messages.views.index')
     35
     36Finally, to view the messages, pointer your browser to
     37/debug_messages/
     38
     39You can clear the file by using the "Clear" button on this web page, or
     40by deleting the file.
     41'''
     42
     43from django.http import HttpResponse
     44from django.shortcuts import render_to_response
     45from django.conf import settings
     46import os
     47import os.path
     48from datetime import datetime
     49
     50#------------------------------------------------------------------
     51# Functions for managing the message file. "write" is the only function
     52# you will need to directly access.
     53
     54def write(message):
     55    fname=get_filename()
     56    try:
     57        fp=file(fname,'a+')
     58        fp.write(str(datetime.now())+':  '+message)
     59        fp.close()
     60    except:
     61        return HTTPHttpResponse(
     62            "Error in DEBUG_MESSAGES: could not write to file: %s\n"% fname+
     63            "Make sure you have write privledges to this folder.")
     64
     65def get_filename():
     66    return os.path.join(settings.DEBUG_MESSAGES_ROOT,'messages.txt')
     67
     68def clear():
     69    fname=get_filename()
     70    if os.path.exists(fname):
     71        os.remove(fname)
     72
     73def read():
     74    fname=get_filename()
     75    if not os.path.exists(fname):
     76        return ['No Messages.']
     77   
     78    fp=file(fname,'r')
     79    the_messages=fp.readlines()
     80    fp.close()
     81    return the_messages
     82 
     83#------------------------------------------------------------------
     84# The view for displaying the messages and clearing the message file.
     85def index(request):
     86
     87    if request.POST.get('clear','')=='clear':
     88        clear()
     89        messages=['No Messages.']
     90    else:
     91        messages=read()
     92
     93    return render_to_response("debug_messages.html",\
     94                              {'messages':messages,
     95                               'site_url':settings.SITE_URL})
     96
     97}}}
     98
     99Create a subfolder called templates. In that folder create a file called "debug_messages.html" and paste the following code into it:
     100
     101
     102{{{
     103<html>
     104<head>
     105<title>Debug Messages</title>
     106</head>
     107
     108<body>
     109<h1>Debug Messages</h1>
     110
     111{% for x in messages %}
     112<p>{{x}}<br><hr></p>
     113{% endfor %}
     114
     115<form method="post" action="{{site_url}}/debug_messages/" >
     116<input type="hidden" name="clear" value="clear" />
     117<input type="submit" value="Clear" />
     118</form>
     119
     120</body>
     121</html>
     122}}}
     123
     124Next, 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.
     125
     126To enable the url for viewing the messages add the following to your urls.py file in your project (not this app):
     127
     128{{{
     129(r'^debug_messages/$', 'yourproject.debug_messages.views.index')
     130}}}
     131
     132To write debugging messages from one of your python scripts, add this import statement to that script:
     133
     134{{{
     135import debug_messages.views as dbm
     136}}}
     137
     138Next, in your script add lines to print messages of the form:
     139
     140{{{
     141dbm.write(your message)
     142}}}
     143
     144To view the messages, point your browswer to the url:
     145
     146root/debug_messages/
     147
     148
Back to Top