Killer Web Development

by Marco Laspe

3.4 Making mistakes

Ok, so far we did everything right.

But most of the time you and me will mak some mistakes. In this case web2py will tell us.

For example lets simulate a typo. This happens even the best programmers. Change the word crud in the second line of the entry_post funtion to crid - missed that u, don't you:

1.
2.
3.
4.
5.
@auth.requires_login()
def entry_post():
"""returns a form where the can entry a post"""
form = crid.create(db.post)
return dict(form=form)

go to http://localhost:8000/pitch/default/entry_post and web2py shows you an error message.

web2py internal error web application

If you click on the link to the ticket, web2py wants you to login with your admin password and then show you the whole error ticket.

The ticket has a lot of information like the Python runtime, the environment variables, etc. The interesting part for us are the error message and the traceback.

web2py error ticket

The error message tells us, that there is something fishy going on with the global name crid. Of course it is, we wrote it intentianally. Unlike crud, crid is not defined is our namespace and for that reason web2py (or better Python) doesn't know what to do with it.

Wikipedia says

Namespace

In general, a namespace is a container that provides context for the identifiers (names, or technical terms, or words) it holds, and allows the disambiguation of homonym identifiers residing in different namespaces.

For many programming languages, a namespace is a context for their identifiers. In an operating system, an example of namespace is a directory. Each name in a directory uniquely identifies one file or subdirectory, but one file may have the same name multiple times.

The traceback shows us where we can find the mistake. In our example it is in file controllers/default.py, line 73, in the function entry_post.

Correct the mistake

Now we could just correct the mistake and we would be done. But for the sake of repetition we will use Mercurial to revert the mistake:

1.
2.
3.
$ hg status #let's which files are affected since last commit
$ hg diff controllers/default.py # let's see what changed
$ hg revert controllers/default.py # revert default.py

Now everything should work as exspected.


Books often read by web2py and Python experts:

Comments

Leave a Reply

Required fields are marked *.