python / django - 为什么我不能在views.py中调用其他函数(在另一个文件中)

时间:2016-07-12 16:06:38

标签: python django

我想使用一些传入参数

从我的views.py中调用generate_pic

在views.py中我有:

def msa_result(request, measurement_id):
    try:
        print measurement_id
        _measurement = UserMeasurements.objects.get(measurement_id=measurement_id)
        import MySQLdb
        db = MySQLdb.connect(host="10.231.XX.XX",  localhost
                           port=3306,
                           user="XXX",  
                           passwd="XXX",  
                           db="XX")  

        print "connect database successfully"
        processing_dict = {'set_id': measurement_id }
        from post_processing import generate_pic
        result = generate_pic(db, processing_dict)
        if result=='success' :
            return render(request, 'msa_result.html', {'measurement': _measurement})
        else:
            raise Http404("Can not process image in post_processing.py")
    except:
        raise Http404("oh noooooo @msa_result")

HTML页面的错误是"哦noooooo @ msa_result"但不是"无法在post_processing.py"

中处理图像 post_proccessing.py中的

我有:

def generate_pic(db, **kwargs):
    print "I an here"  #this never print out 

    if kwargs['set_id']:
        user = kwargs.get('user', 'no_user')
        set_id = kwargs['set_id']
        set_id = int(set_id)
        return 'success'
    else:
        return "Please give a set_id to process result."

views.py和post_processing.py位于同一文件夹中。

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:2)

如果你将dict传递给函数,你必须在**前面将其作为kwargs传递。

result = generate_pic(db, **processing_dict)