django shell< file.py导入功能

时间:2013-09-05 04:50:31

标签: python django sentry

我正在通过一个哨兵命令来尝试:

sentry --config=sentry.conf.py shell < add_user.py

add_user.py看起来像这样:

from django.contrib.auth.models import User

try:
    import authfile
except Exception as e:
    print "authfile not present, %s" % e

def conv_dict(the_dict):
    sen_username = the_dict['sen_username']
    sen_email = the_dict['sen_email']
    sen_password = the_dict['sen_password']
    print sen_username
    return sen_username, sen_email, sen_password

for t, imp_dict in authfile.__dict__.iteritems():
    if isinstance(imp_dict, dict) and not t.startswith('_'):
        sen_username, sen_email, sen_password = conv_dict(imp_dict)
        try:
            new_user = User.objects.create_user( sen_username, sen_email, sen_password)
            new_user.is_superuser = True
            print "created new user: %s" % imp_dict['sen_username']
        except Exception as e:
            print "could not create new user: %s" % e
            pass

authfile是一组字典,用于填充for循环中调用的值。

如果我运行交互式shell并导入此文件,它可以正常工作。如果我尝试使用&lt;将其拉入,它会在没有任何输出的情况下运行并退出,但如果我以交互方式在shell中运行它,它就不会像我那样创建用户。输出如下:

sentry --config=../sentry.conf.py shell < add_super.py
Python 2.7.3 (default, Aug  9 2012, 17:23:57)
Type "copyright", "credits" or "license" for more information.

IPython 1.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]:
In [2]:
In [2]:
In [3]:
In [3]:    ...:    ...:    ...:    ...:    ...:    ...:
In [4]:    ...:    ...:    ...:    ...:    ...:    ...:    ...:    ...:    ...:    ...:
Do you really want to exit ([y]/n)?

0 个答案:

没有答案