| 1 | 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? |
| 2 | |
| 3 | 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. |
| 4 | |
| 5 | |
| 6 | {{{ |
| 7 | ''' |
| 8 | Functions for reading, writing and displaying debugging messages. |
| 9 | |
| 10 | This program writes debugging messages to a text file. In order to |
| 11 | do this, the program must know where to write this file and have write |
| 12 | privelges to the folder that will contain this file. |
| 13 | |
| 14 | To accomplish this, put a variable in your settings.py folder called |
| 15 | DEBUG_MESSAGES_ROOT. Set this variable to the path to the folder where |
| 16 | you want to write the message file. This is what I have in my settings.py |
| 17 | file: |
| 18 | |
| 19 | DEBUG_MESSAGES_ROOT=r'C:\Documents and Settings\CCM\Desktop\pldev\debug_messages' |
| 20 | |
| 21 | Note the last item is a folder name and there is no trailing separator. |
| 22 | |
| 23 | To write debugging messages, in the file where you want to create |
| 24 | messages put: |
| 25 | |
| 26 | import debug_messages.views as dbm |
| 27 | |
| 28 | To post a message call the function: |
| 29 | |
| 30 | dbm.write(your message) |
| 31 | |
| 32 | To enable viewing of the files, add to your urls.py: |
| 33 | |
| 34 | (r'^debug_messages/$', 'yourproject.debug_messages.views.index') |
| 35 | |
| 36 | Finally, to view the messages, pointer your browser to |
| 37 | /debug_messages/ |
| 38 | |
| 39 | You can clear the file by using the "Clear" button on this web page, or |
| 40 | by deleting the file. |
| 41 | ''' |
| 42 | |
| 43 | from django.http import HttpResponse |
| 44 | from django.shortcuts import render_to_response |
| 45 | from django.conf import settings |
| 46 | import os |
| 47 | import os.path |
| 48 | from 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 | |
| 54 | def 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 | |
| 65 | def get_filename(): |
| 66 | return os.path.join(settings.DEBUG_MESSAGES_ROOT,'messages.txt') |
| 67 | |
| 68 | def clear(): |
| 69 | fname=get_filename() |
| 70 | if os.path.exists(fname): |
| 71 | os.remove(fname) |
| 72 | |
| 73 | def 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. |
| 85 | def 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 | |
| 99 | Create 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 | |
| 124 | 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. |
| 125 | |
| 126 | To 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 | |
| 132 | To write debugging messages from one of your python scripts, add this import statement to that script: |
| 133 | |
| 134 | {{{ |
| 135 | import debug_messages.views as dbm |
| 136 | }}} |
| 137 | |
| 138 | Next, in your script add lines to print messages of the form: |
| 139 | |
| 140 | {{{ |
| 141 | dbm.write(your message) |
| 142 | }}} |
| 143 | |
| 144 | To view the messages, point your browswer to the url: |
| 145 | |
| 146 | root/debug_messages/ |
| 147 | |
| 148 | |