Python 实用程序函数应该在 Django 中的什么位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3295268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where should utility functions live in Django?
提问by mitchf
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.
实用程序函数应该在 Django 中的什么位置?自定义加密/解密数字、发送推文、发送电子邮件、验证对象所有权、自定义输入验证等功能。我在应用程序的许多地方使用重复和自定义的东西。我现在肯定正在打破 DRY。
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?
我看到了一些在 models.py 中定义函数的演示,尽管这在我看来在概念上并不正确。他们应该进入导入到我的项目中的“实用程序”应用程序吗?如果是这样,他们在公用事业应用程序中的哪个位置?models.py 文件在那里?
Thanks for helping this n00b out.
感谢您帮助这个 n00b。
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?
更新:让我更具体一点。假设我需要一个函数“light_encrypt(number)”,它接受参数“number”,将其乘以 7,加上 10 并返回结果,另一个函数“light_decrypt(encr_number)”接受参数“encr_number”,减去 10,除以 7 并返回结果。我将把它放在我的 Django 树中的哪个位置?这不是中间件,对吗?正如 Felix 建议的那样,我是否创建了一个 python 包并将其导入到我需要这些函数的视图中?
采纳答案by eruciform
Different questionbut 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 站点布局是:
projects/ templates/ common/ local/在哪里:
- 项目包含您的主要项目和任何其他项目
- common 包含您可以跨站点共享的内容,或者至少不是特定于项目的内容,例如您是否需要下载 django-profile 和 django-registration 而不是直接在 python/site-packages 中使用它
- 模板只包含那个
- local 包含特定于当前机器的内容,以便您可以正确分离数据,例如数据库位置和密码 - 然后我将特定于机器的版本(例如“machine1-localconfig.py”)软链接到local/localconfig.py 然后可以在 settings.py 中“导入 localconfig”
- 我通常将项目特定的中间件放在项目中,而不是项目特定的中间件放在 common/middleware/
- 确保将模板目录添加到设置中的正确位置(或者最有可能是 localconfig.py,然后在设置中导入它),并确保将项目、公共和本地目录添加到您的 PYTHONPATH。
回答by mitchf
OK, after reading the comments and answer here I've decided to create a directory called "common/util/" inside my project directory. Within that I have a file "__ init__.py" where I have my little helper functions.
好的,在阅读了这里的评论和回答后,我决定在我的项目目录中创建一个名为“common/util/”的目录。在其中我有一个文件“__ init__.py”,其中有我的小助手函数。
I guess if the file gets too big, I'll then split out the functions into individual .py files in common. So now, my project structure looks like this. Please correct if I'm making any poor choices, I'm early enough in development that I can fix it now while it is still easy to do so!
我想如果文件变得太大,我会将这些函数拆分为共同的单个 .py 文件。所以现在,我的项目结构是这样的。如果我做出了任何错误的选择,请更正,我的开发还很早,我现在可以修复它,而它仍然很容易做到!
myproject/ (Django project)
common/
util/
__init__.py (helper functions)
middleware/ (custom middleware)
templates/ (project templates)
myapp/
fixtures/ (initial data to load)
migrations/ (south migrations)
urls/
views/
admin.py
forms.py
models.py
test.py
public/ (static stuff not served by Django)
media/
css/
img/
js/
lib/
回答by Josh Petitt
Here is another way to do it:
这是另一种方法:
If the utility functions can be a stand-alone module and you are using a virtualenv environment for your django app then you can bundle the functionality as a package and install it in your virtualenv.
如果实用程序函数可以是一个独立的模块,并且您正在为 django 应用程序使用 virtualenv 环境,那么您可以将该功能捆绑为一个包并将其安装在您的 virtualenv 中。
This makes it easy to import where ever you need it in your django app.
这使得在 django 应用程序中任何需要的地方都可以轻松导入。

