我的Flask
项目结构如下所示:
matcha
|- matcha
|- template
static
__init__.py
matcha.py
user.py
user.py :
class User():
def __init__(self):
db = get_db()
def login(username, password):
...
def register(email, username, firstname, lastname, password):
...
在 matcha.py 中,我创建了user
类的引用,在/login
页面上,我尝试从login()
调用user
方法类文件。
import os
import sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
from . import user
app = Flask(__name__)
app.config.from_object(__name__)
user = User()
...
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
if user.login(username, password):
return redirect(url_for('index'))
else:
error = "Invalid username or password"
return render_template('login.html', error=error)
完整追溯:
File "c:\users\stanc\desktop\matcha\matcha\__init__.py", line 1, in <module>
from .matcha import app
File "c:\users\stanc\desktop\matcha\matcha\matcha.py", line 8, in <module>
user = User()
NameError: name 'User' is not defined
答案 0 :(得分:0)
从我所看到的,您导入的是InvoiceBundle
模块,而不是user
模块中编写的User
类。
因此,您需要重新编写代码,
user
我很确定这会解决你的问题。