python - render_template() takes exactly 1 argument -
i getting 500 error when click submit following view. why getting error , how fix it?
from flask import flask, render_template flask import request, jsonify app = flask(__name__) @app.route('/', methods=['get', 'post']) def homepage(): if request.method == 'post': f1 = request.form['firstverb'] f2 = request.form['secondverb'] return render_template('index.html', f1, f2) return render_template('index.html') if __name__ == "__main__": app.run();
<form class="form-inline" method="post" action=""> <div class="form-group"> <label for="first">first word</label> <input type="text" class="form-control" id="first" name="firstverb"> </div> <div class="form-group"> <label for="second">second word</label> <input type="text" class="form-control" id="second" name="secondverb" > </div> <button type="submit" class="btn btn-primary">run it</button> </form> {{ f1 }} {{ f2 }}
first off when getting 500 error should consider running app debug mode. building app , debugging, no reason keep off hide what's going on you.
if __name__ == "__main__": app.run(debug=true)
now gets more exciting:
127.0.0.1 - - [08/jul/2015 14:15:04] "post / http/1.1" 500 - traceback (most recent call last): ... file "/tmp/demo.py", line 11, in homepage return render_template('index.html', f1, f2) typeerror: render_template() takes 1 argument (3 given)
there problem. should refer documentation render_template
, see accepts 1 positional argument (the template name), rest of arguments (in **context
) provided keyword arguments. otherwise in template there no way reference variables passed in (you have give them explicit names), fixing call to:
return render_template('index.html', f1=f1, f2=f2)
that should solve problems.
finally, please refrain asking questions can trivially solved tiny bit of work (that can gathered better reading of provided documentation), , please go through entire flask tutorial grasp basics of framework.
Comments
Post a Comment