############common.py###########fromdjango.confimportsettingsfromdjango.core.exceptionsimportImproperlyConfiguredimportosapp_dirs=[]forappinsettings.INSTALLED_APPS:i=app.rfind('.')ifi==-1:m,a=app,Noneelse:m,a=app[:i],app[i+1:]try:ifaisNone:mod=__import__(m,{},{},[])else:mod=getattr(__import__(m,{},{},[a]),a)exceptImportError,e:raiseImproperlyConfigured,'ImportError %s: %s'%(app,e.args[0])app_dirs.append(os.path.dirname(mod.__file__))##################mako_django.py#################fromdjango.confimportsettingsfromdjango.core.exceptionsimportImproperlyConfiguredfromdjango.httpimportHttpResponsefromdjango.templateimportTemplateDoesNotExistfromdjango.template.contextimportContextfrommako.lookupimportTemplateLookupfrommako.templateimportTemplatefrommako.exceptionsimportTopLevelLookupExceptionimportosfromcommonimportapp_dirs'''configurations: MAKO_TEMPLATE_DIRS: A tuple, specify the directories in which to find the mako templates, just like TEMPLATE_DIRS . default value is ('mako_templates',) MAKO_MODULE_DIR: A string, if specified, all of the compiled template module files will be stored in this directory. MAKO_MODULENAME_CALLABLE: A callable, if MAKO_MODULE_DIR is not specified, this will be used to determine the filename of compiled template module file. See [http://www.makotemplates.org/trac/ticket/14] Default to the function `default_module_name`, which just appends '.py' to the template filename.'''app_template_dirs=[]forapp_dirinapp_dirs:template_dir=os.path.join(app_dir,'mako_templates')ifos.path.isdir(template_dir):app_template_dirs.append(template_dir)template_dirs=getattr(settings,'MAKO_TEMPLATE_DIRS',None)or('mako_templates',)template_dirs+=tuple(app_template_dirs)defdefault_module_name(filename,uri):''' Will store module files in the same directory as the corresponding template files. detail about module_name_callable, go to http://www.makotemplates.org/trac/ticket/14 '''returnfilename+'.py'module_dir=getattr(settings,'MAKO_MODULE_DIR',None)ifmodule_dir:lookup=TemplateLookup(directories=template_dirs,module_directory=module_dir)else:module_name_callable=getattr(settings,'MAKO_MODULENAME_CALLABLE',None)ifcallable(module_name_callable):lookup=TemplateLookup(directories=template_dirs,modulename_callable=module_name_callable)else:lookup=TemplateLookup(directories=template_dirs,modulename_callable=default_module_name)defselect_template(template_name_list):fortemplate_nameintemplate_name_list:try:returnlookup.get_template(template_name)exceptTopLevelLookupException:passraiseTemplateDoesNotExist,'mako templates: '+', '.join(template_name_list)defget_template(template_name):try:returnlookup.get_template(template_name)exceptTopLevelLookupException:raiseTemplateDoesNotExist,'mako templates: '+template_namedefrender_to_response(template_name,dictionary=None,context_instance=None):ifisinstance(template_name,(list,tuple)):template=select_template(template_name)else:template=get_template(template_name)dictionary=dictionaryor{}ifcontext_instanceisNone:context_instance=Context(dictionary)else:context_instance.update(dictionary)data={}[data.update(d)fordincontext_instance]returnHttpResponse(template.render(**data))###################genshi_django.py##################fromdjango.confimportsettingsfromdjango.httpimportHttpResponsefromdjango.templateimportTemplateDoesNotExistfromdjango.template.contextimportContextfromgenshi.templateimportMarkupTemplate,TemplateLoaderfromgenshi.template.loaderimportTemplateNotFoundimportosfromcommonimportapp_dirs'''configuration: GENSHI_TEMPLATE_DIRS: specify directories in which to find the genshi template files. default value is ('genshi_templates',)'''app_template_dirs=[]forapp_dirinapp_dirs:template_dir=os.path.join(app_dir,'genshi_templates')ifos.path.isdir(template_dir):app_template_dirs.append(template_dir)template_dirs=getattr(settings,'GENSHI_TEMPLATE_DIRS',None)or('genshi_templates',)template_dirs+=tuple(app_template_dirs)loader=TemplateLoader(template_dirs,auto_reload=settings.DEBUG)defselect_template(template_name_list):fortemplate_nameintemplate_name_list:try:returnloader.load(template_name)exceptTemplateNotFound:passraiseTemplateDoesNotExist,'genshi templates: '+', '.join(template_name_list)defget_template(template_name):try:returnloader.load(template_name)exceptTemplateNotFound:raiseTemplateDoesNotExist,'genshi templates: '+template_namedefrender_to_response(template_name,dictionary=None,context_instance=None):ifisinstance(template_name,(list,tuple)):template=select_template(template_name)else:template=get_template(template_name)dictionary=dictionaryor{}ifcontext_instanceisNone:context_instance=Context(dictionary)else:context_instance.update(dictionary)data={}[data.update(d)fordincontext_instance]stream=template.generate(**data)returnHttpResponse(stream.render('xhtml'))

More like this

Comments

Hello! Nice code, but if I may allow a suggestion:
the mako TemplateLookups should allow to specify default_filters if the data is non-ascii (I use default_filters=['decode.utf_8'] ); and similarly, the mako template.render should be
template.render_unicode(**data).encode('utf-8', 'replace')