【发布时间】:2020-08-31 14:00:59
【问题描述】:
我正在学习用于构建 Web 应用程序的烧瓶库。当我实现时我遇到了错误。我已经解决了我自己的问题但没有工作它只是显示表单没有发布我的博客。请帮助我解决这个问题.我会很感激的。 我有一个 python 文件和两个 html 文件 还有一个 forms.db 文件。
application.py
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///forms.db'
db = SQLAlchemy(app)
class blog(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100) , nullable=False)
content = db.Column(db.Text , nullable=False)
author = db.Column(db.String(20) , nullable=False)
def __repr__(self):
return 'blog post ' + str(self.id)
@app.route("/", methods=['GET','POST'])
def formation():
if request.method == 'POST':
show_title = request.form.get('title')
show_content = request.form.get('content')
show_author = request.form.get('author')
new_blog = blog(title=show_title, content=show_content, author=show_author)
db.session.add(new_blog)
db.session.commit()
return redirect('/')
else:
allP = blog.query.all()
return render_template('formpage.html',posts=allP)
if __name__ == '__main__':
app.run(debug=True)```
formpage.html
base.html
【问题讨论】:
-
它可以帮助我们将您的代码内联