python逻辑与界面分离,Django:将业务逻辑与视图逻辑分离

In my Django app, I am trying to figure out where to include my business logic. The logic does not fit any models(assume it's a one-page app that doesn't have any models) so I want to add a module that holds the logic. For example,

project/app/my_logic.py

def calculate(number_one, number_two):

return number_one + number_two

Then, I would use the logic like,

project/app/views.py

def index(request):

number = my_logic.calculate(1, 2) #can I do this?

return HttpResponse("the number is: %s " % number)

Questions:

Where is the right place to put my_logic.py?

Is it conventional?

What might be a better way?

Note: this is how you import your module (if anyone else is trying to figure out how to do it)

project/app/your_module/your_module.py

project/app/your_module/__init__.py

from views.py,

from app.your_module import your_module

解决方案

Depends, but if it's a logical unit, then a separate python module would be a good start and you would of course write its own unit tests for it (as opposed to Django's built-in integration tests with TestClient).

If it needs to work within the model, then a property on model would be a better place.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值