0
votes

I am doing the flask training course enter link description here

I'm install Flask-SQLAlchemy in a virtual environment. I'm install Flask-Migrate: pip install flask-migrate

Created a class config.py:

import os
basedir = os.path.abspath(os.path.dirname(__file__))

class Config(object):
    SECRET_KEY = os.environ.get('SECRET_KEY') or '*********'    
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///' + os.path.join(basedir, 'app.db')  
    SQLALCHEMY_TRACK_MODIFICATIONS = False

In models.py I have two models:

from datetime import datetime
from app import db

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    email = db.Column(db.String(120), index=True, unique=True)
    password_hash = db.Column(db.String(128))
    posts = db.relationship('Post', backref='author', lazy='dynamic')

    def __repr__(self):
        return '<User {}>'.format(self.username)

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    body = db.Column(db.String(140))
    timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    def __repr__(self):
        return '<Post {}>'.format(self.body)

In testapp.py only:

from app import app

In file init.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from config import Config

app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)

from app import routes, models

Structure my app:

testapp\
  venv\
  app\
    templates\
        base.html
        index.html
        login.html 
    __init__.py
    routes.py
    forms.py
    models.py    
  testapp.py
  config.py

In a Python interpreter session, do the following from app.models import User and get an error:

(venv) C:\Users\User\testapp>python
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 
64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> from app.models import User
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "C:\Users\User\testapp\app\__init__.py", line 4, in <module>
     from config import Config
   File "C:\Users\User\testapp\config.py", line 8
     SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
                                                                 ^
TabError: inconsistent use of tabs and spaces in indentation
 >>>

Set the environment variable for the new config module set FLASK_APP=config.py

When I execute the command:

flask db init

Complete error log:

(venv) C:\Users\User\testapp>flask db init
Usage: flask db init [OPTIONS]

Error: Failed to find Flask application or factory in module "config". Use "FLAS
K_APP=config:name to specify one.

(venv) C:\Users\User\testapp>

What am I doing wrong? Help

1
can you post the complete error log?Tech at The Sparks Foundation
First error is the inconsistent tabs error i config.py due to '/' in your code where you are defining sql_database_uri you dont need '/' for defining or condition. Can you try running without this?Tech at The Sparks Foundation
Now it's only about inconsistent tabs.. Can you check for indentations?Tech at The Sparks Foundation
Run this command in cmd export FLASK_APP=testapp.py before flask db initTech at The Sparks Foundation
@ Tech at The Sparks Foundation Thanks a lot, it works for me. I have windows and so I ran the set FLASK_APP = testapp.py command, after which the migration repository was successfully created. Thanks again.Ambasador

1 Answers

1
votes

Run this command in cmd

export FLASK_APP=testapp.py

before

flask db init