6

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

1
  • 1
    You have a typo in your configuration code. It is SQLALCHEMY_DATABASE_URI not SQLALCHEMY_DATABASE_URL. Commented Oct 20, 2022 at 18:46

1 Answer 1

10

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.