[1.7.x] Fixed #23060 -- Prevented UnicodeDecodeError in debug templatetag

Backport of 08451f17d0 from master.