Python Forum

Full Version: got some problem with flask signup form.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi
I just made a login och signup forms for my webapp its nothing advanced. I got the login to work and connect to the database but i cant get the signup part working. The app is using 2 databases.

I get and error message saying.

Error:
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user [SQL: 'INSERT INTO user (username, pass_hash) VALUES (?, ?)'] [parameters: ('xzenon', 'sha256$Q3VMxPLv$cb1c8b17a0e970cec485e432450d06eb016012203ed2fc5207dd74153b033f89')] (Background on this error at: http://sqlalche.me/e/e3q8)
I dont know if this part is connecting to wrong database. Im new at using multiple databases in flask.

Error:
line 43, in signup new_user = User(username=username, pass_hash=hashed_pwd) db_session.add(new_user) try: line 43--> db_session.commit() except sqlalchemy.exc.IntegrityError: flash("Username {u} is not available.".format(u=username)) return redirect(url_for('signup')) flash("User account has been created.")
The code is .

main.py
@app.route("/signup/", methods=["GET", "POST"])
def signup():

    if request.method == "POST":
        username = request.form['username']
        password = request.form['password']

        if not (username and password):
            flash("Username or Password cannot be empty")
            return redirect(url_for('signup'))
        else:
            username = username.strip()
            password = password.strip()

        # Returns salted pwd hash in format : method$salt$hashedvalue
        hashed_pwd = generate_password_hash(password, 'sha256')

        new_user = User(username=username, pass_hash=hashed_pwd)
        db_session.add(new_user)

        try:
            db_session.commit()
        except sqlalchemy.exc.IntegrityError:
            flash("Username {u} is not available.".format(u=username))
            return redirect(url_for('signup'))

        flash("User account has been created.")
        return redirect(url_for("login"))

    return render_template("signup.html")
models.py
class User(db.Model):
    __tablename__ = "user"
    __bind_key__ = 'logindb'

    uid = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(100), unique=True, nullable=False)
    pass_hash = db.Column(db.String(100), nullable=False)

    def __repr__(self):
        return '' % self.username
app.py
from flask import Flask
from flask_sqlalchemy import sqlalchemy, SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///Matrialdb.db'
app.config["SQLALCHEMY_BINDS"] = {"logindb": 'sqlite:///auth.db'}
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.secret_key = "flask rocks!"
app.debug = True

db = SQLAlchemy(app)
Does any one know what my problem is?
Hi. Did you migrate your models?

# db_migrate.py
from app import db, create_app

db.create_all(app=create_app())
I have a python file like below to create databases and its tables.

You should use factory methods.

Flask Factory Methods