Django分离业务逻辑和视图逻辑

Django分离业务逻辑和视图逻辑

本文介绍了Django分离业务逻辑和视图逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释以下含义吗?

Could someone please explain what the following means:

每个示例的一般区别是什么以及几个示例.谢谢你.

What is the general distinction for each as well as a few examples. Thank you.

推荐答案

您可能刚刚在另一个问题的评论中提问了;).

You could've just asked in the comments on the other question ;).

业务逻辑是与事物"的工作或操作方式相关的任何事物.例如以下内容:

Business logic is anything that's related to how a "thing" works or operates. Take for example the following:

class Animal(Object):
    def speak(self, sound):
        print sound

class Duck(Animal):
    has_feathers = True

做类似的事情是不正确的:

It would be incorrect to do something like:

>>> myduck = Duck()
>>> myduck.speak('Quack!')
Quack!

鸭子发出嘎嘎"声的事实.是业务逻辑,应该在模型中:

The fact that a duck makes the sound of 'Quack!' is business logic, and should be in the model:

class Duck(Animal):
    has_feathers = True
    makes_sound = 'Quack!'

    def speak(self):
        super(Duck, self).speak(self.makes_sound)

您不一定需要了解所有这些内容;我们要做的就是确保Duck讲话时说"Quack!":

You don't necessarily need to understand all that; all we're doing is ensuring that when a Duck speaks it says 'Quack!':

>>> myduck = Duck()
>>> myduck.speak()
Quack!

查看逻辑将与处理请求并返回某种响应有关.使用前面的示例,我们的视图将包含实例化Duck对象并使之说话的代码.

View logic would be anything related to processing a request and returning some sort of response. Using the previous example, our view would contain the code to instantiate a Duck object and make it speak.

myduck = Duck()
myduck.speak()

响应"就是嘎嘎!".

这篇关于Django分离业务逻辑和视图逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 08:50
查看更多