Python Forum
got some problem with flask signup form.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
got some problem with flask signup form.
#1
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?
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask: editing a submitted form test 1 1,701 Jun-07-2022, 10:37 AM
Last Post: test
  Flask Basics request.form ifigazsi 0 1,783 Feb-09-2021, 09:05 AM
Last Post: ifigazsi
  Flask request.form ifigazsi 2 3,571 Feb-02-2021, 11:56 AM
Last Post: buran
  problem with the return of flask loutsi 4 2,054 Jun-04-2020, 08:12 AM
Last Post: loutsi
  Simple flask rest api problem cancerboi 4 2,782 Jan-29-2020, 03:10 PM
Last Post: brighteningeyes
  form.populate_obj problem "object has no attribute translate" pascale 0 3,621 Jun-12-2019, 07:30 PM
Last Post: pascale
  how i save the html form to flask database mebaysan 1 7,244 Feb-07-2019, 12:56 AM
Last Post: snippsat
  Django allauth saving custom user profile fields with signup form prithvi 1 10,462 Aug-08-2017, 03:51 PM
Last Post: prithvi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020