我正在学习用于构建Web应用程序的Flask库。当我意识到自己遇到了bug时。我已经解决了自己的问题但不起作用,只是显示了表单未发布我的博客。请帮助我解决此问题我会很感激的。 我有一个python文件和两个html文件 和一个form.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
答案 0 :(得分:0)
您需要输入submit
类型的输入
<input type="submit" value="post"/>
代替type="button"