python - Flask-Sqlite posting to DB from webform -
i have started learning flask , tried find answer how post sqlite db webform. far haven't managed work , bit lost this. manage print values db based on code sample simplypython don't know how add new ones webform.
i need able address elements, open connection database, insert values, save , close connection. far aware should add post method app.py , use request.form statement pull elements when submit button pressed.
then code should automatically display values on index html, works. please me code need add app.py file values added db , add form action webform-section on html file?
index.html
<!doctype html> <html> <head> <title>flask intro</title> <!-- <meta name="viewport" content="width=device-width, initial-scale=1.0"> --> </head> <body> <div class="container"> <h3 potsit </h3> {% post in posts %} titleotsikko: {{post.title }} <br> postotsikko: {{post.description}} {% endfor %} </div> <div> <form action="/????????????not sure direct" method="post"> <div> <label for="title">title:</label> <input type="text" id="title" /> </div> <div> <label for="description">description:</label> <input type="text" id="description" /> </div> <div class="button"> <button type="submit">add db</button> </div> </form> </div> </body> </html>
app.py
from flask import flask, render_template, request, session, g import sqlite3 app = flask(__name__) @app.route('/') def home(): g.db = sqlite3.connect("sample.db") cur = g.db.execute('select * posts') posts = [dict(title=row[0], description=row[1]) row in cur.fetchall()] g.db.close() return render_template("index.html", posts=posts) if __name__=='__main__': app.run(debug=true)
sql.py
import sqlite3 sqlite3.connect("sample.db") connection: c = connection.cursor() c.execute("drop table posts") c.execute("create table posts(title text, description text)") c.execute('insert posts values("luck", "no luck.")')
edit
i made changes based on suggestion paul rooney , created file called post.html, moved form there index.htmland added @route on app.py file. believe i'm not far off after trying figure out since tuesday hoping work. unfortunately receive error 405 'the method not allowed requested url.' , i'm stuck after trying different options.
post.html
<!doctype html> <html> <head> <title>flask post</title> <!-- <meta name="viewport" content="width=device-width, initial-scale=1.0"> --> </head> <body <div> <form action="/post" method="post"> <div> <label for="title">title:</label> <input type="text" id="title" /> </div> <div> <label for="description">description:</label> <input type="text" id="description" /> </div> <div class="button"> <button type="submit">add db</button> </div> </form> </div> </body> </html>
(edited) app.py
from flask import flask, render_template, request, session, g, redirect, url_for import sqlite3 app = flask(__name__) @app.route('/') def home(): g.db = sqlite3.connect("sample.db") cur = g.db.execute('select * posts') posts = [dict(title=row[0], description=row[1]) row in cur.fetchall()] g.db.close() return render_template("index.html", posts=posts) @app.route('/post', methods=['post']) def post(): title=request.form['title'] description=request.form['description'] print title, description return redirect(url_for('/')) if __name__=='__main__': app.run(debug=true)
to answer original question should pick endpoint post data , use in both flask app , html form action.
i suggested post
in comments anything. think you've grasped here completeness.
the html
<form action="/post" method="post">
the python code
add route handle posted data.
@app.route('/post', methods=['post']) def post(): # db stuff redirect index page. pass
edited question
you have indentation error in code, causing 405 error. post
function inside home
function. make more this
app.py
from flask import flask, render_template, request, session, g, redirect, url_for import sqlite3 app = flask(__name__) @app.route('/') def home(): g.db = sqlite3.connect("sample.db") cur = g.db.execute('select * posts') posts = [dict(title=row[0], description=row[1]) row in cur.fetchall()] g.db.close() return render_template("index.html", posts=posts) # 1 level of indentation removed here downwards @app.route('/post', methods=['post']) def post(): title=request.form['title'] description=request.form['description'] return redirect(url_for('home')) if __name__=='__main__': app.run(debug=true)
after see 400 bad request
error, stems fact don't have name
parameters in html form.
the attempt access title
, description
values in form
dict in app.py throw keyerror
exception, keys not present without name
parameter in html.
if add them e.g.
... <input type="text" id="title" name='title'/> ... <input type="text" id="description" name='description'/> ...
then run way through function.
the next issue
redirect(url_for('/'))
instead use
redirect(url_for('home'))
home
being name of function called path '/'.
after should go.
Comments
Post a Comment