By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,956 Members | 1,683 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,956 IT Pros & Developers. It's quick & easy.

Flask - Sqlite Unable to send data to DB from webform

P: 1
I have struggled to get this code to work for some days now. This should be just simple web form where text is added to 2 fields and posted to the Sqlite-database.

I am able to display data from db to index.html but accessing below address results to 405 error -'The method is not allowed for the requested URL' when I try to access
http://localhost:5000/post. What is wrong with app.py file likely relating to @app.route('/post', methods=['POST']) and its method?

app.py
Expand|Select|Wrap|Line Numbers
  1. from flask import Flask, render_template, request, session, g, redirect, url_for
  2. import sqlite3
  3.  
  4.  
  5. app = Flask(__name__)
  6.  
  7.  
  8. @app.route('/')
  9.  
  10. def home():
  11.       g.db = sqlite3.connect("sample.db")
  12.       cur = g.db.execute('select * from posts')
  13.       posts = [dict(title=row[0], description=row[1]) for row in cur.fetchall()]
  14.       g.db.close()
  15.       return render_template("index.html", posts=posts)
  16.  
  17.  
  18. @app.route('/post', methods=['POST'])
  19. def post():
  20.     title=request.form['title']
  21.     description=request.form['description']
  22.     return redirect(url_for('/post'))
  23.  
  24. if __name__=='__main__':
  25.   app.run(debug=True)
  26.  
post.html
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <title>Flask post</title>
  5. <!--  <meta name="viewport" content="width=device-width, initial-scale=1.0">  -->
  6.   </head>
  7.   <body
  8.  
  9. <div>
  10.  
  11. <form action="/post" method="post">
  12.     <div>
  13.         <label for="title">title:</label>
  14.         <input type="text" id="title" />
  15.     </div>
  16.     <div>
  17.         <label for="description">description:</label>
  18.         <input type="text" id="description" />
  19.     </div>
  20.  
  21.  
  22.     <div class="button">
  23.         <button type="submit">Add to db</button>
  24.     </div>
  25. </form>
  26. </div>
  27.  
  28.  
  29.   </body>
  30. </html>
  31.  
sql.py
Expand|Select|Wrap|Line Numbers
  1. import sqlite3
  2.  
  3. with sqlite3.connect("sample.db") as connection:
  4.   c = connection.cursor()
  5.   c.execute("DROP TABLE posts")  
  6.   c.execute("CREATE TABLE posts(title TEXT, description TEXT)")
  7.   c.execute('INSERT INTO posts VALUES("this is title", "this is description.")')
  8.  
Oct 16 '15 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 613
Try something like this to verify that the select is working
Expand|Select|Wrap|Line Numbers
  1.     c.execute('select * from posts')
  2.     recs_list=c.fetchall()
  3.     for row in recs_list:
  4.         ## print it to verify that the select is correct
  5.         print row 
as I think the insert statement is the problem and should be
Expand|Select|Wrap|Line Numbers
  1. c.execute('INSERT INTO posts values (?,?)', ("this is title", "this is description.")')
Oct 16 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.