mardi 15 septembre 2015

Error passing an html variable to a function in flask

I am trying to register a user into a sqlite database. My form contains a hidden field with a numerical value.

When I process the form, it returns the following error: "The browser (or proxy) sent a request that this server could not understand."

Which apparently means a variable is not passed to the function.

How could I address the problem please? I have tried changing the data type to int() in the backend before sending to the function, I have made the field visible etcetc. Nothing worked.

Funny enough: when I use the "print" function in the backend and disable the "User.regUser(email, username, password, rank)", their values shows in the console... so I would assume the variables contain the info I need to pass to the function.

Here is the code:

Views:

@app.route('/register', methods=['GET', 'POST'])
def register():
    #if request.method == 'POST':encrypt
    email = request.form['email']
    username = request.form['username']
    password = generate_password_hash(request.form['pass1'])
    rank = int(request.form['rank'])

    print(email, password, username, rank)
    User.regUser(email, username, password, rank)

#    db.session.add(User(email, username, password, "50"))
#    db.session.commit()
    return render_template('register.html')

Models:

from pjctBB.views import db


class User(db.Model):

    __tablename__ = "users"

    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String, nullable=False)
    username = db.Column(db.String, nullable=False)
    password = db.Column(db.String, nullable=False)
    rank = db.Column(db.Integer, nullable=False)

    def __init__(self, email, username, password, rank):
        self.email = email
        self.username = username
        self.password = password
        self.rank = rank

    def regUser(self, email, username, password, rank):

        db.session.add(User(email, username, password, rank))
        db.session.commit()

Thanks a bunch!

Aucun commentaire:

Enregistrer un commentaire