如何检查Django中是否存在模板?

时间:2022-10-16 19:22:02

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?

检查Django中是否存在模板的最有效方法是什么?我正在考虑捕获TemplateDoesNotExist异常,但也许有更多的Djangoistic方法来做到这一点?

Thanks for your help!

谢谢你的帮助!

3 个解决方案

#1


19  

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...)

我认为如果不捕获此异常,您将无法执行此操作,但您可以在try语句中使用django.template.loader.get_template(template_name),而不是使用render_to_response的乐观主义调用。 (如果你还没有这样做......)

#2


30  

If your intention is to use a template if it exists and default to a second template, you would better use select_template:

如果您打算使用模板(如果存在)并默认使用第二个模板,那么最好使用select_template:

django.template.loader.select_template(['custom_template','default_template'])

This will load the first existing template in the list.

这将加载列表中的第一个现有模板。

#3


6  

Here is what I implemented, which goes off of Fabio's answer. I don't know if this is the best way to do this, but it works as expected for me.

这是我实施的内容,它取决于法比奥的答案。我不知道这是否是最好的方法,但它对我来说是预期的。

from django.views.generic import TemplateView
from django.http import Http404
from django.template.loader import get_template
from django.template import TemplateDoesNotExist
from absolute.menu.models import Menu # specific to my app

class BasicPublicView(TemplateView):
    model = Menu #specific to my app

    def dispatch(self, request, *args, **kwargs):
        try:
            self.template_name = request.path[1:] + '.html'
            get_template(self.template_name)
            return super(BasicPublicView, self).dispatch(request, *args, **kwargs)
        except TemplateDoesNotExist:
            raise Http404

This allows me to dynamically pull a template from the templates directory if the template exists. For example, http://example.com/products/keyboards will attempt to fetch the template /templates/products/keyboards.html

如果模板存在,这允许我从templates目录动态提取模板。例如,http://example.com/products/keyboards将尝试获取模板/templates/products/keyboards.html

#1


19  

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...)

我认为如果不捕获此异常,您将无法执行此操作,但您可以在try语句中使用django.template.loader.get_template(template_name),而不是使用render_to_response的乐观主义调用。 (如果你还没有这样做......)

#2


30  

If your intention is to use a template if it exists and default to a second template, you would better use select_template:

如果您打算使用模板(如果存在)并默认使用第二个模板,那么最好使用select_template:

django.template.loader.select_template(['custom_template','default_template'])

This will load the first existing template in the list.

这将加载列表中的第一个现有模板。

#3


6  

Here is what I implemented, which goes off of Fabio's answer. I don't know if this is the best way to do this, but it works as expected for me.

这是我实施的内容,它取决于法比奥的答案。我不知道这是否是最好的方法,但它对我来说是预期的。

from django.views.generic import TemplateView
from django.http import Http404
from django.template.loader import get_template
from django.template import TemplateDoesNotExist
from absolute.menu.models import Menu # specific to my app

class BasicPublicView(TemplateView):
    model = Menu #specific to my app

    def dispatch(self, request, *args, **kwargs):
        try:
            self.template_name = request.path[1:] + '.html'
            get_template(self.template_name)
            return super(BasicPublicView, self).dispatch(request, *args, **kwargs)
        except TemplateDoesNotExist:
            raise Http404

This allows me to dynamically pull a template from the templates directory if the template exists. For example, http://example.com/products/keyboards will attempt to fetch the template /templates/products/keyboards.html

如果模板存在,这允许我从templates目录动态提取模板。例如,http://example.com/products/keyboards将尝试获取模板/templates/products/keyboards.html