【问题标题】:why my flask app is not posting the posts in formpage.html?为什么我的烧瓶应用程序没有在 formpage.html 中发布帖子?
【发布时间】: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

【问题讨论】:

  • 它可以帮助我们将您的代码内联

标签: python html flask jinja2


【解决方案1】:

你需要一个submit 类型的输入:

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

而不是type="button"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 2017-05-01
    • 2022-06-10
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    相关资源
    最近更新 更多