hi.
I made a table showing user accounts in my app and i tought it would be a good thing to add an edit function to it so i can edit the accounts. Every thing works till i get to the saveing part of the edit function there i get this error
Could any one help me with this? I can provide more code if this i post is not enough.
main.py
This is for the table.
I made a table showing user accounts in my app and i tought it would be a good thing to add an edit function to it so i can edit the accounts. Every thing works till i get to the saveing part of the edit function there i get this error
Error:ProgrammingError: SQLite objects created in a thread can only be used in that same thread.The object was created in thread id and this is thread
Something like that. The funny thing i get this error on my laptop but not on my stationary but also nothing happens there i can spam the save button and nothing happens all i see in the console is POST msg.Could any one help me with this? I can provide more code if this i post is not enough.
main.py
This is for the table.
@app.route('/show_users') @login_required(role="admin") def show_users(): results = [] qry = db_session.query(User) results = qry.all() if not results: flash('Inget kunde hittas!') return redirect('adminindex') else: # display results table = Users(results) table.border = True return render_template('show_users.html', table=table)this is for the account edit function.
@app.route('/redigera_anvandare/<int:id>', methods=['GET', 'POST']) @login_required(role="admin") def redigera_anvandare(id): """ Add / edit an item in the database """ qry = db_session.query(User).filter( User.id==id) anvandare = qry.first() if anvandare: form = UserForm(formdata=request.form, obj=anvandare) if request.method == 'POST' and form.validate(): # save edits save_changes3(anvandare, form) flash('Artikeln har blivit uppdaterad!') return redirect('/adminindex') return render_template('redigera_användare.html', form=form) else: return 'Error loading #{id}'.format(id=id)Forms.py
class UserForm(FlaskForm): user = StringField('Användarnamn:', validators=[DataRequired()]) password = StringField('Lösenord:', validators=[DataRequired()]) #owner = StringField('Konto ägare:', validators=[DataRequired()]) urole = RadioField('Användar Behörighet:', choices=[('admin', 'administratör'),('user', 'Användare')]) submit = SubmitField("Sign In")models.py
class User(db.Model): __tablename__ = "anvandare" id = db.Column(db.Integer, primary_key=True) user = db.Column(db.String(80), unique=True) password = db.Column(db.String(80)) #owner = db.Column(db.String(80)) urole = db.Column(db.String(80)) def __init__(self, user, password, urole): self.user = user self.password = password #self.owner = owner self.urole = urole def __repr__(self): return '<User %r>' % self.user def is_authenticated(self): return True def is_active(self): return True def is_anonymous(self): return False def get_id(self): return str(self.user)db_setup.py
from sqlalchemy import create_engine from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.ext.declarative import declarative_base engine = create_engine('sqlite:///Matrialdb.db', connect_args={'check_same_thread': False}, convert_unicode=True) db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine)) Base = declarative_base() Base.query = db_session.query_property() def init_db(): import models Base.metadata.create_all(bind=engine)