1.I'm trying to build a flask project and try to import the class into models.py and import routes into app.py. When I was trying to run the project, it showed some errors. Here is the information for traceback:
Traceback (most recent call last):
File "/Users/cheliang/Desktop/project/env/lib/python3.8/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/Users/cheliang/Desktop/project/app.py", line 4, in <module>
from user import routes
File "/Users/cheliang/Desktop/project/user/routes.py", line 1, in <module>
from user.models import User
ImportError: cannot import name 'User' from 'user.models' (/Users/cheliang/Desktop/project/user/models.py)
The tree structure for my project:
├── app.py
├── env
── run
├── static
│ ├── css
│ │ ├── normalize.css
│ │ └── styles.css
│ └── js
│ ├── jquery.js
│ └── scripts.js
├── templates
│ ├── base.html
│ ├── dashboard.html
│ └── home.html
└── user
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-38.pyc
│ ├── models.cpython-38.pyc
│ └── routes.cpython-38.pyc
├── models.py
└── routes.py
2.Here is the code details: models.py:
from flask import Flask, jsonify
class User:
def signup(self):
user = {
"_id":"",
"name":"",
"email":"",
"password":""
}
return jsonify(user),200
Here is the routes.py:
from user.models import User
from flask import Flask
from app import app
@app.route('/user',methods=["GET"])
def signup(self):
return User().signup()
Here is the code for app.py
from flask import Flask ,render_template
app = Flask(__name__)
from user import routes
@app.route('/')
def home():
return render_template('home.html')
@app.route('/dashboard')
def dashboard():
return render_template('dashboard.html')
For a minimalist flask application, consider this kind of structure:
project_folder
|---------- app.py
|---------- config.py
|---------- .env
|---------- requirements.txt
|---------- .flaskenv
|---------- app/
|------ routes.py
|------ models.py
|------ __init__.py
|------ forms.py
|------ templates/
|-------- home.html
|-------- base.html
|-------- dashbooard.html
|------ static/
|-------css/
|------- styles.css
|------- normalize.css
|-------js/
|------- jquery.js
|------- scripts.js
Create application instance:
# __init__.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
db = SQLAlchemy(app)
from app import routes, models
Working with flask models:
# models.py
from app import db
class User(object):
username = db.Column(db.String())
# ...
# routes.py
from flask import render_template
from app import app
from app.models import User
from app.forms import <your-form>
@app.route('/')
def home():
return render_template('home.html')
@app.route('/dashboard')
def dashboard():
return render_template('dashboard.html')
@app.route('/user',methods=["GET"])
def signup():
# ...
user = User.query.filter_by(username=form.username.data)
Finally, create an entry point to your flask application in the app.py
:
from app import app
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With