I can’t seen to understand what the issue here is, I am getting an error that local variable ‘allLibrary’ referenced before assignment, Please help
@app.route('/', methods =['GET','POST'])
def add():
if request.method =='POST':
bookName=(request.form['bookName'])
authorName =(request.form['author'])
genre = request.form['type']
library = library(bookName=bookName, authorName=authorName, genre = genre)
db.session.add(library)
db.session.commit()
allLibrary = library.query.all()
return render_template('index.html', allLibrary=allLibrary)
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The UnboundLocalError: local variable referenced before assignment error is raised when you try to assign a value to a local variable before it has been declared. You can solve this error by ensuring that a local variable is declared before you assign it a value.
So to fix this make sure that the allLibrary variable is declared before you’re accessing it. It could also be a Scope problem.
However it would seem that library is not assigned a value if the if statement fails, so you should probably ensure that that is within scope as well.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0