View function did not return a response

前端 未结 1 718
陌清茗
陌清茗 2020-12-10 04:48

I want to send a query to mysql and fetch an array. But however I do it I cannot make it work. Here\'s my code:

@app.route(\'/auth\',methods=[\'GET\',\'POST\         


        
相关标签:
1条回答
  • 2020-12-10 04:56

    Flask throws this exception because your auth view didn't return anything. Return a response from your auth view:

    return 'Some response'
    

    To return the MySQL results, perhaps join the rows together into one string:

    cur.execute("SELECT * FROM tbl_user WHERE username = '%s' " % username)
    return '\n'.join([', '.join(r) for r in cur])
    

    or define a template and return the rendered template.

    Note that you really do not want to use string interpolation for your username parameter, especially in a web application. Use SQL parameters instead:

    cur.execute("SELECT * FROM tbl_user WHERE username = %s", (username,))
    

    Now the database client will do the quoting for you and prevent SQL injection attacks. If you use string interpolation, this will happen.

    (If this was a decent database (e.g. not MySQL) the database could take the now-generic SQL statement and create a query plan for it, then reuse the plan again and again as you execute that query multiple times; using string interpolation you'd prevent that.)

    0 讨论(0)
提交回复
热议问题