python - 实用程序函数在 Django 中应该放在哪里?

标签 python django structure

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

我看到了一些在 models.py 中定义函数的演示,尽管这在我看来在概念上并不正确。他们应该进入导入到我的项目中的“实用程序”应用程序吗?如果是这样,它们在实用程序应用程序中的位置是什么?那里的models.py文件?

感谢您帮助这个 n00b。

更新:让我说得更具体一些。假设我需要一个函数“light_encrypt(number)”,它接受参数“number”,将其乘以 7,加 10 并返回结果,另一个函数“light_decrypt(encr_number)”接受参数“encr_number”,减去 10,除以 7 并返回结果。我会将它放在我的 Django 树的什么位置?这不是中间件,对吗?正如 Felix 建议的那样,我是否创建一个 python 包并将其导入到我需要这些函数的 View 中?

最佳答案

不同 question但同样的答案:

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.

关于python - 实用程序函数在 Django 中应该放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3295268/

相关文章:

django - 如何使其成为整数?

python - 对于每组对 REST API 函数的调用,仅发送一封电子邮件

C++从文件填充结构数组

python - Django Rest 框架中的最佳实现 follow 和 unfollow

django - django如何在基于类的 View 中获取响应

python - 如何根据标准图像校正图像的颜色

arrays - MATLAB:将字段添加到 for 循环中的结构

c - 自动打印 C 中的结构和变量

python - 如何禁用 libpng 警告? ( python ,游戏)

python - 当 Flask 中出现表单错误时,消息不会闪烁