实用程序功能应该在Django中存在? [英] Where should utility functions live in Django?

查看:150
本文介绍了实用程序功能应该在Django中存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实用程序功能应该在Django中存在?诸如自定义加密/解密数字,发送推文,发送电子邮件,验证对象所有权,自定义输入验证等功能。我在应用程序中的许多地方使用的重复和自定义的东西。我现在肯定打破了DRY。



我看到一些演示,其中功能在models.py中定义,虽然这似乎在概念上对我来说是正确的。他们应该进入我的项目导入的实用程序应用程序吗?如果是这样,他们在公用程序应用程序中去哪里?这个models.py文件在那里?



感谢您帮助这个n00b。



更新: / strong>让我更具体一点。说我需要一个函数light_encrypt(number),它使用参数number,将它乘以7,加10并返回结果,另一个采用参数encr_number的函数light_decrypt(encr_number)减去10,除了7,并返回结果,在我的Django树中,我会把它放在哪里?这不是中间件,对吗?如Felix所说,我要创建一个python包,并将其导入到需要这些功能的视图中?

解决方案

不同问题但同样的答案:


我通常的django网站的布局是:

  projects / 
templates /
common /
本地/

其中:




  • 项目包含您的主要项目和任何其他项目

  • common包含t你可能会在网站上分享,或者至少不是项目特定的,如果您需要下载django-profile和django注册,而不是直接在python / site-packages中

  • 模板仅包含

  • 本地包含将要针对当前机器的内容,以便您可以正确分离数据,如数据库位置和密码 - 然后软链接机器特定的版本(比如machine1-localconfig.py)到local / localconfig.py然后可以在settings.py

  • 中导入localconfig我通常把中间件这个项目 - 项目中的具体内容,以及在普通/中间件/

  • 中不是项目特定的中间件,请确保将templates目录添加到设置中的正确位置(或最可能的是localconfig.py和然后在设置中导入它),并确保将项目,公用和本地目录添加到您的PYTHONPATH。



Where should utility functions live in Django? Functions like custom encrypting/decrypting a number, sending tweets, sending email, verifying object ownership, custom input validation, etc. Repetitive and custom stuff that I use in a number of places in my app. I'm definitely breaking DRY right now.

I saw some demos where functions were defined in models.py, although that didn't seem conceptually right to me. Should they go in a "utilities" app that gets imported into my project? If so, where do they go in the utilities app? The models.py file there?

Thanks for helping this n00b out.

UPDATE: Let me be even more specific. Say I need a function "light_encrypt(number)" which takes the param "number", multiplies it by 7, adds 10 and returns the result, and another function "light_decrypt(encr_number) which takes the param "encr_number", subtracts 10, divides by 7 and returns the results. Where in my Django tree would I put this? This is not middleware, right? As Felix suggests, do I create a python package and import it into the view where I need these functions?

解决方案

Different question but same answer:

My usual layout for a django site is:

projects/
templates/
common/
local/

Where:

  • projects contains your main project and any others
  • common contains things you may share across sites, or are at least not project-specific, like if you need to download django-profile and django-registration rather than having it directly in python/site-packages
  • templates contains just that
  • local contains things that are going to be specific to the current machine, so that you can have properly separated data, like database location and password - I then soft-link the machine-specific versions (say "machine1-localconfig.py") to local/localconfig.py and then can "import localconfig" in settings.py
  • I generally put middleware that's project-specific inside a project, and middleware that's not project-specific in common/middleware/
  • make sure to add the templates directory to the right place in settings (or most probably, localconfig.py and then import it in settings), and makse sure to add the projects, common, and local directories to your PYTHONPATH.

这篇关于实用程序功能应该在Django中存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆