Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use only some parts of Django?

Tags:

python

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.

Specifically, I want to use:

  • 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.

I would not use:

  • Templating
  • urlconfigs

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

Can this work?

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.

like image 627
Hanno Fietz Avatar asked Nov 19 '08 17:11

Hanno Fietz


Video Answer


2 Answers

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' 

Then in your regular Python code, do

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

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.

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 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.

like image 133
Eli Courtwright Avatar answered Sep 22 '22 10:09

Eli Courtwright


Django, being a web framework, is extremely efficient at creating websites. However, it's also equally well-suited to tackling problems off the web. This is the loose coupling that the project prides itself on. Nothing stops you from installing a complete version of Django, and just using what you need. As a rule, very few components of Django make broad assumptions about their usage.

Specifically:

  • Django models don't know anything about HTML or HTTP.
  • Templates don't know anything about HTML or HTTP.
  • The cache system can be used to store anything that can be pickled.

One of the main things you'll face when trying to use Django without a web server is setting up the environment properly. The ORM and cache system still need to be configured in settings.py. There are docs on using django without a settings module that you may find useful.

like image 35
user21799 Avatar answered Sep 22 '22 10:09

user21799