Django:存储项目特定中间件的最理想的地方是什么? [英] Django: What is the most ideal place to store project-specific middleware?

查看:111
本文介绍了Django:存储项目特定中间件的最理想的地方是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以存储在我的python路径的任何地方,而且我可以使用什么组织的模式来存储我的项目的中间件类?

I'm aware I can "store it anywhere in my python path" and all, but what's an organized pattern I can use to store middleware classes for my project?

我通过mod_wsgi将我的项目根目录和项目目录添加到sys路径:

I am appending my project root directory and project directory to the sys path through mod_wsgi:

sys.path.append( '/srv/' )
sys.path.append( '/srv/workarounds/' )

后一行是项目根。举个例子,假设我要应用这个中间件类: http://djangosnippets.org/snippets/1179/

The latter line being the project root. As an example, let's say I want to apply this middleware class: http://djangosnippets.org/snippets/1179/

是否将代码段内容复制到middleware.py文件中并将其转储到我的项目根目录中?为中间件创建一个目录,将该目录添加到我的python路径?

Would I just copy the snippet contents into a middleware.py file and dump it in my project root? Create a directory for middleware, add that directory to my python path?

推荐答案

我通常的django站点的布局是: p>

My usual layout for a django site is:

projects/
templates/
common/
local/

其中:


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

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

  • 模板仅包含

  • 本地包含将要针对当前机器的内容,以便您可以正确分离数据,如数据库位置和密码 - 然后将机器特定版本(例如machine1-localconfig.py)软链接到本地​​/ localconfig.py,然后在settings.py中import localconfig li>
  • 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

我一般来说p在项目中是项目特定的中间件,而中间件不是通用/中间件/

I generally put middleware that's project-specific inside a project, and middleware that's not project-specific in common/middleware/

确保将templates目录添加到设置中的正确位置(或者很可能是localconfig.py然后在设置中导入它),并确保将项目,常见和本地目录添加到您的PYTHONPATH。

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天全站免登陆