Sunday, November 28, 2010

View getting called twice for each GET

I've been playing around with django, building a toy app just to get
the feel of it. I've been going nuts trying to figure out why my
index method has been getting called in all sort of places I didn't
expect. I finally figured it out and thought I would share this with
the group (hopefully to prevent other django newbies from making the
same mistake).

I had a catch-all url defined:

urlpatterns = patterns('',
(r'^db/query/mn_id/', 'db.views.query_nm_id'),
(r'^db', 'db.views.index'),
(r'', 'db.views.index'),
)

I was seeing index() being called twice when I did a GET on "/db", and
if I did a GET on "/db/query/mn_id", I'd see query_mn_id() get called,
followed by a call to index(). It turns out the second GET in both
cases was my browser (Chrome on OSX) asking for "/favicon"! Duh.

The clue that set me on the right path was that if I reloaded the page
with the "Reload this page" button, I got the behavior described
above, but if I navigated my history with the back and forward arrows,
I didn't get the extra call to index(). I little sleuthing with
tcpdump showed what was going on, and logging request.get_full_path()
in each view method verified this.

What's really weird is that the /favicon GET isn't logged by
development server (python manage.py runserver). For example,
runserver prints just:

[28/Nov/2010 09:36:57] "GET /db/query/ HTTP/1.1" 200 5

but I see in my application log:

DEBUG:root:/db/query/
DEBUG:root:/favicon.ico

Why don't both requests get printed?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment