Skip to content


MVC Blasphemy

I just put HTML code into my data model. I have a list-of-objects page. Each object is an instance of an object defined in my data model, derived from a row in a database. Each object needs a pretty link drawn that object’s detailed-view page. So I added a property on my object:

class Message(SQLObject):
    def _get_view(self):
        "Draw a link to the view page for this message."
        return cElementTree.XML("""<a href="/details/%d">VIEW</a>""" % self.id)
    # Lots of other stuff snipped out.

This is now what my kid template looks like:

        <tr py:for="m in messages">
            <td py:for="col in columns" py:content="getattr(m, col)">MESSAGE STUFF</td>

I pass in messages and columns; messages is a list of objects and columns is a tuple of strings that map to attributes or properties, like “view”.

I’m happy with this decision. I know I could have manipulated the messages or created some new classes in my controller, but I couldn’t really see any advantage. This way works.

I just don’t want anyone else doing this :)

Posted in Programming, Python, TurboGears, web.

 
close Reblog this comment
blog comments powered by Disqus