如何检查Django中是否存在模板? [英] How to check if a template exists in Django?

查看:192
本文介绍了如何检查Django中是否存在模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查Django中是否存在模板,最有效的方法是什么?我正在考虑捕捉 TemplateDoesNotExist 异常,但也许有更多的Djangoistic方法可以实现?



谢谢为您的帮助!

解决方案

我不认为你可以做到这一点没有捕捉这个例外,但你可以使用 django.template您的try语句中的.loader.get_template(template_name) ,而不是乐观主义的render_to_response调用。 (如果你还没有这样做...)


What is the most efficient way to check if a template exists in Django? I was thinking of catching the TemplateDoesNotExist exception, but maybe there is a more Djangoistic way to do it?

Thanks for your help!

解决方案

I don't think you'll be able to do this without catching this exception, but you could use django.template.loader.get_template(template_name) in your try statement instead of a optimist call of render_to_response. (If you are not already doing this...)

这篇关于如何检查Django中是否存在模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆