python - Flask add DB entry using a form -
i'm beginner learning flask. i'm making app , i've created db model user, , html/ js form takes input. want use form information create new entry in database unsure on how it. tried
@app.route('/add_to_db') def add_to_db(): email = request.form['email'] activated = 0; user = models.user(email= email, activated = 0) db.session.add(user) db.session.commit()
html code:
<form onsubmit="return validateemail(document.getelementbyid('email').value)" action="{{ url_for("add_to_db") }}" method="post"> please input email adress: <input id="email"> <input type="submit"> </form> <script> function validateemail(email) { var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i; return(re.test(email)); } </script>
but gave me 405 method not allowed error.
a 405 error means "method not allowed". sending form data, using post request , need allow post requests. default requests allowed. change line @app.route('/add_to_db')
@app.route('/add_to_db', methods=['post'])
.
Comments
Post a Comment