Created
August 20, 2009 07:29
-
-
Save veryhappythings/170893 to your computer and use it in GitHub Desktop.
Get the template for a view in django.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Maybe I'm missing the point, but coming from a bit of | |
# rails experience I find that it's a pain that views | |
# have to be told which template to render to. | |
# This method is a way, but probably not the right way, | |
# to derive a template name from an object. | |
# It expects to be called from a view, by the way. | |
import inspect | |
def template_for(obj, content_type='html'): | |
return '{app}/{obj}_{func_name}.{content_type}'.format(app=obj._meta.app_label, | |
obj=obj._meta.object_name.lower(), | |
func_name=inspect.stack()[1][3], | |
content_type=content_type) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment