0

I am inserting the data in database using Flask SQLAlchemy Everything is going fine when I insert data using "/" in action attribute code but when I use to redirect to action="users.html" page after posting the data I get the message "the method is not allowed for requested url in users page and data does not save too"

<form method="POST" action="/">
    Name <input type="text" name="name">
    <br>
    Password <input type="text" name="email">
    <br>
    <input type="submit">
</form>


class Users(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)



@app.route("/", methods = ['GET', 'POST'])
def contact():
    if(request.method=='POST'):
        '''Add entry to the database'''
        name = request.form.get('name')
        email = request.form.get('email')
        entry = Users(name=name, email=email)
        db.session.add(entry)
        db.session.commit()
    return render_template('index.html')

@app.route('/users')
def users():
    return render_template('users.html')
  • Possible duplicate of [Flask - POST - The method is not allowed for the requested URL](https://stackoverflow.com/questions/34853033/flask-post-the-method-is-not-allowed-for-the-requested-url) – SuperShoot Oct 10 '19 at 08:54

1 Answers1

0

Try adding methods=['POST'] to:

@app.route('/users', methods=['POST'])
def users():
    return render_template('users.html')
Emad
  • 71
  • 1
  • 3