This looks like you're doing it wrong. Why the dangling </p> just to chomp it afterwards? and why the string of html and the html_safe upon it all? Can you have a go at making all the html part of the template? I think it would clean this up.