Django:在视图或模板中计算更有效?

发布于 2024-10-10 00:41:03 字数 592 浏览 3 评论 0原文

几乎可以肯定以前已经有人问过这个问题,所以如果重复的话,我们深表歉意。但我找不到答案:)

在 Django 中,一般来说,在视图中还是在模板中进行计算效率更高?

这是一个简单的例子。我想在模板中放置一个特定的字符串,具体取决于整数的值。我可以在 views.py 中执行此操作:

# in views.py
description = "small"
if count > 10:
    description = "large"
elif count > 5:
    description = "medium"

或者我可以在模板中执行此操作:

# in template.html
{{ count }} 
({% if count > 10 %}large
{% else %}
{% if count > 5 %}medium{% else %}small{% endif %}
{% endif %})

在这种情况下,视图中的代码明显更简单,所以也许这回答了我的问题:但我真正要做什么我想知道的是,在模板或视图中执行此操作对效率有影响吗?

This has almost certainly been asked here before, so apologies if it's a duplicate. I can't find the answer though :)

In Django, in general, is it more efficient to do calculations in the view, or in the template?

Here's a simple example. I want to put a particular string in the template, dependent on the value of an integer. I could do it in views.py:

# in views.py
description = "small"
if count > 10:
    description = "large"
elif count > 5:
    description = "medium"

Or I could do it this way in the template:

# in template.html
{{ count }} 
({% if count > 10 %}large
{% else %}
{% if count > 5 %}medium{% else %}small{% endif %}
{% endif %})

In this case, the code is noticeably simpler in views, so perhaps that answers my question: but what I really want to know is, does it make a difference to efficiency to do it in either the template or the view?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晌融 2024-10-17 00:41:03

这取决于你所说的高效是什么意思。

前一个版本涉及较少的函数调用,因为当逻辑包含在模板中时,在执行之前必须首先将字符串解析为节点并标记化。

然而,这两者的性能在现实世界中是相同的,因为它们都非常简单。

对效率的更广泛的解释将包括代码的可维护性,当代码包含在视图(或模型,如果适用)中时,通常更具可读性并且更易于重构。

无论哪种解释,最好将尽可能多的逻辑从模板移出并移至视图、控制器或模板标记中。

It depends what you mean by efficient.

The former version involves fewer function calls, because when logic is contained in a template, the strings must first be parsed into nodes and tokenized, before being executed.

However, the performance of both of those will be identical in a real-world context, as they are both very simple.

A broader interpretation of efficiency would include the maintainability of code, which is generally more readable and more easy to refactor when contained in the view (or model, where appropriate).

In either interpretation, it's best to move as much logic as possible out of the template and into view, controllers, or templatetags.

表情可笑 2024-10-17 00:41:03

我不会说这是效率问题,但更多的是代码适合使用 MVC 类似模式。如果这都是关于如何显示某些内容而不是显示什么,那么模板肯定是一个合适的地方。如果您说要修改显示的内容,那么视图是更合适的地方!

如果您认为此值与模型深度绑定,那么您还可以在模型上创建一个从模板内调用的方法。如果您在其他地方需要相同的功能,那么制作 自定义模板标签用于此目的!

但请记住 django 对于处理问题并没有那么严格原始的 MVC 模式,例如。 templatetags 在模板中引入了很多功能!

I wouldn't say that this is a question of efficiency, but more of where the code fits using a MVC like pattern. If this is all about how to display something and not what the template is a suitable place for sure. If you'd say you modify what is displayed the view is the more suitable place!

If you'd say that this values are bound deeply to the model, you could also make a method on the model that you call from within the template. If you need the same functionality at other places it could also make sense to make a custom template tag for this purpose!

But keep in consideration that django isn't so strict about dealing with the original MVC pattern, and eg. templatetags pull a lot of functionality inside the template!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文