-1

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!

choubix
  • 59
  • 1
  • 7
  • Maybe it fails to find a key in the form dictionary. Show us the `register.html`. – lord63. j Sep 16 '15 at 14:05
  • Hi lord63. There is no "register. Html" template actually. I am passing the form to the function Def register(). – choubix Sep 17 '15 at 07:31
  • Hi lord63. There is no register. Html template, I am passing the form to the function Def register (). If I completely bypass "rank" (remove it from the form, the function, the database...), the form works though. – choubix Sep 17 '15 at 07:33

2 Answers2

0

From what I gather, you're trying to request from a form without first rendering it, meaning there is no form to get or post from. Try this:

@app.route('/register', methods=['GET', 'POST'])
def register():
    if request.mthod == 'GET':
               return render_template('register.html')

    elif request.method == 'POST':

              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')

That's assuming register.html exists.

Simon Melouah
  • 642
  • 4
  • 11
  • 24
0

Thank you all for your interest in my question. It got me thinking a bit :)

I asked my question slighly differently on SO and 2 users gave me a solution. I ncase someone runs into a similar problem as mine, please look this thread for a possible solution:

Flask registration error: missing 1 positional argument

Community
  • 1
  • 1
choubix
  • 59
  • 1
  • 7