Error in Python: IndentationError: unindent does not match any outer indication level

Asked 1 years ago, Updated 1 years ago, 215 views

There is an error in the initialization part of DB, but I don't know why it is.

This situation cannot be solved even after many revisions and rewrites.
Please tell me the cause and solution.

def__init__(self, title, body, date):

app.py Runtime Error Messages

File"C:\work\dev\FLASK\CURD\app.py", line32
    def__init__(self, title, body, date):
                                          ^
IndentationError—unindent does not match any outer indication level
 from flash import Flask, render_template
from flash_sqlalchemy import SQLAlchemy
from datetime import datetime
from pytz import timezone

app = Flask(__name__)

app.config ['SECRET_KEY'] = 'mysecretkey'
URI='sqlite://note.db'
app.config ['SQLALCHEMY_DATABASE_URI'] = URI
app.config ['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)


from sqlalchemy.engine import Engine
from sqlalchemy import event

@ event.listens_for (Engine, "connect")
default_sqlite_pragma(dbapi_connection, connection_record):
    cursor=dbapi_connection.cursor()
    cursor.execute("PRAGMA foreign_keys=ON")
    cursor.close()


class Note (db.Model):
     __tablename__='notes'
     id=db.Column(db.Integer, primary_key=True)
     title=db.Column(db.String(140))
     body=db.Column(db.String(300))
     date=db.Column(db.DateTime, default=datetime.now(timezone('Asia/Tokyo'))))

    def__init__(self, title, body, date): # Error here
        self.title=title
        self.body=body
        self.date = date

@ app.route('/')
def index():
    title='List Screen'
    return render_template ('index.html', title=title)

if__name__=='__main__':
    app.run(debug=True)

python

2023-01-15 05:45

1 Answers

class Note(db.Model): The indentation between the line and the line def__init__(self, title, body, date): is one digit more.
It should work by subtracting one digit of the indentation space for that part.

class Note (db.Model):
     __tablename__='notes'####From here↓
     id=db.Column(db.Integer, primary_key=True)
     title=db.Column(db.String(140))
     body=db.Column(db.String(300))
     date=db.Column(db.DateTime, default=datetime.now(timezone('Asia/Tokyo'))))#### That's it ↑

    def__init__(self, title, body, date): # Take the error here
        self.title=title
        self.body=body
        self.date = date


2023-01-15 07:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.