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')