Skip to content

Instantly share code, notes, and snippets.

@veryhappythings
Created August 20, 2009 07:29
Show Gist options
  • Save veryhappythings/170893 to your computer and use it in GitHub Desktop.
Save veryhappythings/170893 to your computer and use it in GitHub Desktop.
Get the template for a view in django.
# 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