开发者

Is it better to perform logical conditions in view or template?

开发者 https://www.devze.com 2023-03-25 21:28 出处:网络
I\'m starting to learn Django and I\'m completely new to the MVC/MTV (whatever you want to call it) playground.

I'm starting to learn Django and I'm completely new to the MVC/MTV (whatever you want to call it) playground.

In Django templates can perform logical conditions and filters. I want to know from a performance standpoint, is it better to do the conditions in the view or on the template itself.

I actually find my self more comfortable with doing everything behind t开发者_StackOverflowhe scenes and if there is no difference in terms of performance, then I'd rather do it from the view.


I suggest separating your logic in the following way:

  • Implement the logic belonging to what your application is doing under the hood in your Django handlers. It usually covers most of your logic.

  • Implement only the logic belonging to the actual presentation in your HTML templates. For example you can put you logic selecting different styles for various rows of a table based on a complex condition (not just odd/even) or so.

It worked pretty well for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消