2

I want to call generate_pic from my views.py with some pass-in parameters

In views.py I have:

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")

The HTML page's error is "oh noooooo @msa_result" but not "Can not process image in post_processing.py"

in post_proccessing.py I have:

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 and post_processing.py are in the same folder.

What's wrong with my code?

3
  • 3
    Take away that bare except clause. All you're doing is hiding whatever the problem actually is. Let Django report it to you, then you can fix it. Commented Jul 12, 2016 at 16:19
  • @DanielRoseman the error is TypeError: generate_pic() takes exactly 1 argument (2 given) What should I change.. Commented Jul 12, 2016 at 16:31
  • 1
    To add on what @DanielRoseman said. Wrapping large portions of code inside try-except clauses are a really bad practice. It will hide problems and make debugging a nightmare. Usually this pattern is used by people new to programming as a way to make problems "just go away". Commented Jul 12, 2016 at 16:33

1 Answer 1

2

Where you pass the dict into the function you have to prepend ** to pass it in as kwargs.

result = generate_pic(db, **processing_dict)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.