So like most dummies, when I needed debug my django application I was peppering “print” statements throughout my code. It was very helpful to find out what was going on, but a pain to remember where I had put all my print statements. This was especially true when I was trying to debug classes within other classes.

Then I found the python logging module. You get the same output of the print command, but logging allows you to decide when to output the information.

In your django settings file add the lines:

import logging
if DEBUG:

logging.basicConfig(level=logging.DEBUG)

The above code is importing the logging module and setting the logging level to debug if the DEBUG setting is set to ‘True’.

Now to use in your code, just ‘import logging’ and call logging for the output you want to trace.

Super simple example:

import logging

def test_it(request):

if request.method == ‘POST’:

postDict = request.POST.copy()

logging.debug(‘From your post %s’, postDict)

return postDict

{ 0 comments }

Stack Overflow : A Q&A tool

May 5, 2010

A great resource for Django Dummies (or dummies of any language) is Stack Overflow. Stack Overflow is a free community site designed for programmers and software engineers. With a wide range of topics in computer programming  this Q & A site is a free place for all kind of programmers who can learn here some programming types, suggest new tricks and see others experinces. [...]

Read the full article →

Sites powered by Django

March 19, 2010

Developed four years ago Django is a high-level web framework written in Python which follows the model-view-controller architectural structure. Django comes across as an impressive web development platform complete with files, settings, and data models. The main advantage of using this web framework is that Django eases the creation of otherwise complex and database driven [...]

Read the full article →

New POST and IP checking tool

post parrot ip check March 4, 2010

Post Parrot allows developers to send POST variables to the home page and it echos back what was in the post. It also show the client IP and headers that were sent.

Read the full article →

Tornado Web Server

February 27, 2010

Tornado is an open source version of scalable, non-blocking web server and web framework. It was developed for use by FriendFeed and later in 2009 the company also was acquired by Facebook. Tornado is noted for its high performance and is written using a web framework that looks a bit like web.py or Google’s webapp. [...]

Read the full article →

Django-paypal and M2Crypto on webfaction

September 29, 2009

If you are using django-paypal on Webfaction and want to encrypt the post form, you are going to run into an issue building M2Crypto since their servers OpenSSL has been built without EC support.  You can get around this by using the following commands to install M2Crypto: cat >> ~/.pydistutils.cfg << EOF [build_ext] include_dirs=/usr/include/openssl EOF [...]

Read the full article →

Why you need help text in your objects

August 23, 2009

Adding docstrings can make your life much easier in the long run.  They can also help anyone that might be looking at your code after you. Here is a simple example of a model with a doc string: class total_clicks(models.Model): “””Here are the total clicks that have come from this user. “”” user = models.ForeignKey(User, [...]

Read the full article →

What is in python object?

August 23, 2009

When I made the move from being a PHP dummy to a Django dummy, one of the biggest issues I ran into was trying to debug my code. In PHP you can echo or loop echo pretty much anything and see what data you have to deal with. When I started with django I could not for the life of me figure out what each object included and how to get at it. Then I found python’s dir().

Read the full article →