python - How do you load information into a database using flask? -
trying teach myself flask making web app. i'm having trouble posting inputs user database , when load page , try submit info through form 405 error:
"get / http/1.1" 200 -,
"post / http/1.1" 405 -
any insight appreciated, thanks.
here python snippet:
session = dbsession() app = flask(__name__) @app.route('/') def index(methods=['get','post']): print request.method if request.method == 'post': instances = session.query(vocab) newitem = vocab(id=len(instances), word=request.form['new_word']) session.add(newitem) session.commit() instances = session.query(vocab) return render_template('vocab_template.html', instances = instances)
the html template:
<!doctype html> <html> <head> <title>vocab</title> </head> <body> <div> <h1>words!</h1> <ul id='words'> {% in instances %} <li> {{i.word}} </li> {% endfor %} </ul> <form action="/" method="post"> <input type="text" name='new_word'> <input type="submit" value="add" name='submit'> </form> </div> </body> </html>
you close
@app.route('/',methods=['get','post']) def index(): print request.method ...
Comments
Post a Comment