4

I am learning basic flask but cannot import db(database) from freecodecamp I cannot find the solution plz answer this query as soon as possible There are 4 files : when I write from app import db the pythonshell shows error

app.py

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

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

class Todo(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    content = db.Column(db.String(200), nullable = False)
    date_created = db.Column(db.DateTime, default = datetime.utcnow)

    def __repr__(self):
        return '<Task %r>' % self.id

@app.route('/')
def index():
    return render_template('index.html')

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

main.css

body {
    margin: 0;
    font-family: sans-serif;
}

base.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width-device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie-edge">
    <link rel="stylesheet" href= "{{ url_for('static', filename = 'css/main.css')}}">
      
    {% block head %}{% endblock %}
</head>
<body>
    {% block body %}{% endblock %}
</body>
</html>

index.html

{% extends 'base.html'%}

{% block head %}

{% endblock %}


{% block body %}
<h1>Template</h1>
{% endblock %}

Terminal Window

davidism
  • 121,510
  • 29
  • 395
  • 339
  • 1
    You have a typo in your configuration code. It is `SQLALCHEMY_DATABASE_URI` not `SQLALCHEMY_DATABASE_URL`. – Detlef Oct 20 '22 at 18:46

1 Answers1

5

You need to initialize db as well,

db = SQLAlchemy() # db intitialized here
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://test.db"
db.init_app(app)

It's SQLALCHEMY_DATABASE_URI not SQLALCHEMY_DATABASE_URL

Rahul K P
  • 15,740
  • 4
  • 35
  • 52