本文介绍了只使用 Django 的某些部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢 Django,但对于一个特定的应用程序,我只想使用它的一部分,但我对 Django 在内部的工作方式还不够熟悉,所以也许有人可以为我指出正确的方向我必须退房.

I like Django, but for a particular application I would like to use only parts of it, but I'm not familiar enough with how Django works on the inside, so maybe someone can point me into the right direction as to what I have to check out.

具体来说,我想要使用:

  • 模型和数据库抽象
  • 缓存 API,虽然我想通过缓存来避免数据库查找,而不是 HTML 生成,而且由于 Django 中的缓存框架是为后者设计的,我还不确定这是否真的合适.
  • The models and database abstraction
  • The caching API, although I want to avoid database lookups by caching, not HTML generation, and since the caching framework in Django is intended for the latter, I'm not sure yet whether that's really appropriate.

我不会不使用:

  • 模板
  • urlconfigs

或者,更准确地说,我既不使用 HTTP 也不使用 HTML.所以基本上,我有一个与平时不同的输入/输出链.

Or, more exactly, I'm neither using HTTP nor HTML. So basically, I have a different input / output chain than usual.

这能行吗?

我在 Django 中的个人杀手级功能是我可以对模型进行对象/数据库映射,所以如果有另一种技术(不一定是 Python,我处于设计阶段,我很不可知)关于语言和平台)给我同样的能力,那也很棒.

My personal killer feature in Django is the Object / database mapping that I can do with the models, so if there's another technology (doesn't have to be Python, I'm in the design phase and I'm pretty agnostic about languages and platforms) that gives me the same abilities, that would be great, too.

推荐答案

我自己使用 Django 进行对象/数据库映射,而没有使用它的 urlconfigs.只需创建一个名为 djangosettings.py 的文件并插入必要的配置,例如:

I myself use Django for its object/db mapping without using its urlconfigs. Simply create a file called djangosettings.py and insert the necessary configuration, for example:

DATABASE_ENGINE   = 'oracle'
DATABASE_HOST     = 'localhost'
DATABASE_NAME     = 'ORCL'
DATABASE_USER     = 'scott'
DATABASE_PASSWORD = 'tiger'

然后在您的常规 Python 代码中,执行

Then in your regular Python code, do

import os
os.environ["DJANGO_SETTINGS_MODULE"] = "djangosettings"

在导入任何 Django 模块之前.这将使您无需实际拥有 Django 项目即可使用 Django 的对象/数据库映射,因此您可以将其用于独立脚本或其他 Web 应用程序或任何您想要的.

before you import any Django modules. This will let you use Django's object/db mappings without actually having a Django project, so you can use it for standalone scripts or other web applications or whatever you want.

至于缓存,如果您不想使用 Django,那么您可能应该决定使用什么并从那里开始.我推荐使用 CherryPy,它不使用 Django 风格的正则表达式 URL 映射,而是根据函数名称自动将 URL 映射到函数.CherryPy 主页顶部有一个示例:http://cherrypy.org/

As for caching, if you don't want to use Django then you should probably decide what you are using and go from there. I recommend using CherryPy, which doesn't use Django-style regular expression URL mapping, but instead automatically maps URLs to functions based on the function names. There's an example right at the top of the CherryPy home page: http://cherrypy.org/

CherryPy 有自己的缓存系统,因此您可以完成与 Django 完全相同的事情,而无需使用 Django 的 urlconfig 系统.

CherryPy has its own caching system, so you can accomplish exactly the same thing as what Django does but without needing to use Django's urlconfig system.

这篇关于只使用 Django 的某些部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 16:20
查看更多