为什么我的烧瓶应用程序没有在formpage.html中发布帖子?

时间:2020-08-31 05:57:24

标签: python html flask jinja2

我正在学习用于构建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

this is my formpage.html image.i think code is not working due to this html file..

base.html this is base.html file i just extends this to formpage.html..

1 个答案:

答案 0 :(得分:0)

您需要输入submit类型的输入

<input type="submit" value="post"/>

代替type="button"