单独运行一个Django程序

我需要运行一个单独的里程,定时从我的Django工程User数据库中取出信息,作一些操作。比如每天看是否有用户超时,等等。 因为你不是通过apache + mod_python 去访问的,所以需要手动在程序的开始import settings,  有两种方式:
1.Python语言:
from django.conf import settings
settings.configure(
DATABASE_ENGINE = 'sqlite3',
DATABASE_NAME = '/var/www/vpn_ssh/proxy/database/register.db',
DATABASE_USER = '', # Not used with sqlite3.
DATABASE_PASSWORD = '', # Not used with sqlite3.
DATABASE_HOST = '', # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '', # Set to empty string for default. Not used with sqlite3
AUTH_PROFILE_MODULE = 'register.SSH_User',
)
内容从你原先工程里copy过来就OK了,要啥copy啥。
2. Python语言
import django.core.management
from foo import settings
django.core.management.setup_environ(settings)
直接import 你原先的settings,但我总把路径搞错,就采取了第一种比较直白的方式了。。。

   上面的搞定,就可以从原先工程里读取到用户数据了。不过我的工程里User使用profile进行扩展了,得到user对象后需要使用user.get_profile来取一下,结果取到个异常——SiteProfileNotAvailable, google半天, 貌似是需要在settings里加一个常量
AUTH_PROFILE_MODULE = 'register.SSH_User'
直接加到文件头部的数组里,加完跑了一下,还是这个错。。。挠头。。。

 看看原先的工程里settings里面是没有这个值的,只在使用的时候,import register.SSH_User.

模仿一把,把前面AUTH_PROFILE_MODULE从settings里去掉,加了个import。 还是有错。

纳闷老半天,又把AUTH_PROFILE_MODULE加上, 最终,二者同时存在的时候可以了。。。

3 评论:

Anonymous said...

Excellent blog post. I certainly love this website. Thanks!



Check out my website :: dental implants cost

Anonymous said...

Its not my first time to pay a visit this web page, i am browsing this site dailly and take fastidious facts from here all the
time.

Review my blog: cost of dental implant

Anonymous said...

whoah this weblog is great i like studying your posts.
Keep up the good work! You recognize, a lot of persons are searching round for this information, you can
aid them greatly.

Feel free to surf to my homepage ... how much does a root canal cost