我在同一目录示例中有flaskblog.py和forms.py。但是当我尝试“从表单导入RegistrationForm,LoginForm”代码时,我在PyCharm中编译了时间错误。
我成功地能够这样导入:从sample.forms导入RegistrationForm,LoginForm
此后,当我尝试使用python flaskblog.py运行代码时,出现以下错误:
(venv) C:\Users\ksrihari\PycharmProjects\myFlaskStudy\sample>python flaskblog.py
Traceback (most recent call last):
File "flaskblog.py", line 2, in <module>
from sample.forms import RegistrationForm, LoginForm
ModuleNotFoundError: No module named 'sample'
from flask import Flask, render_template, url_for
from sample.forms import RegistrationForm, LoginForm
import datetime
app = Flask(__name__)
app.config['SECRET_KEY'] = 'a515c030528fbddb08443cea4e6fd96c'
posts = [
{
'author':'Corey Schafer',
'title':'Blog Post1',
'content':'First Blog Post',
'date_posted':'18, May 2019',
'last_updated_date':f"{datetime.datetime.now()::%d, %b %Y}"
},
{
'author':'Srihari K S S',
'title':'Blog Post2',
'content':'Second Blog Post',
'date_posted':'18, May 2019',
'last_updated_date':f"{datetime.datetime.now()::%d, %b %Y}"
}
]
@app.route("/")
@app.route("/home")
def home():
return render_template('home.html', posts=posts)
@app.route("/about")
def about():
return render_template('about.html', title='About')
@app.route("/register")
def register():
form = RegistrationForm()
return render_template('register.html', form = form, title = 'Register')
@app.route("/login")
def register():
form = LoginForm()
return render_template('login.html', form = form, title = 'Login')
if __name__ == '__main__':
app.run(debug=True)[enter image description here][1]
答案 0 :(得分:0)
如果它是一个程序包(带有 init .py文件的目录),请按以下方式导入它:
from package_name.forms import ClassName
如果它不是软件包,而只是单独的模块,则可以这样导入:
from forms import ClassName
干杯!