Home > Mobile >  NameError: name 'allTodos' is not defined | UnboundLocalError: local variable 'allTod
NameError: name 'allTodos' is not defined | UnboundLocalError: local variable 'allTod

Time:11-28

Hi I am doing practice on flask, creating Todo App. If I use global after

db.session.add(todo)

then I get an NameError If I dont use global keyword then I get the UnboundLocalError: Here is my code:

from flask import Flask, render_template ,request
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///todo.db'
db = SQLAlchemy(app)


class Todo(db.Model):
    SNumber = db.Column(db.Integer,primary_key=True)
    title = db.Column(db.String(200),nullable=False)
    desc = db.Column(db.String(500),nullable=False)
    data_created = db.Column(db.DateTime, default = datetime.utcnow)

    def __repr__(self) -> str:
        return f"{self.SNumber} - {self.title}"


@app.route('/' , methods=['GET','POST'])
def hello_world():
    if request.method == 'POST':
        title = request.form['title']
        desc = request.form['desc']
        todo = Todo(title = title, desc = desc)
        db.session.add(todo)
        db.session.commit()
        
        allTodos = Todo.query.all()
        
    return render_template('index.html',allTodo=allTodos)
        
    

@app.route('/show')
def products():
    #allTodo = Todo.query.all()
    #print(allTodo)
    return "this is a product page"    

if __name__ == "__main__":
    app.run(debug=True)


CodePudding user response:

Your variable is defined and assigned in the if statement. There is a chance that code block will not be executed, resulting in the interpreter not knowing what to do when it reaches the return statement without having "all_todos" defined. You can always define it and assign it a default value before the if statement.

CodePudding user response:

Thanks for your help but it resolves with indentation.

  • Related