这是我的python代码:
from flask import Flask , render_template , request , redirect , url_for
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
#Making sql
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///posts.db'
db = SQLAlchemy(app)
class BlogPost(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, default='N/A')
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
def __repr__(self):
return 'Blog Post' + str(self.id)
''' You can put mltiue links for the website '''
@app.route("/")
def index():
return render_template('index.html')
@app.route('/posts', methods=['GET', 'POST'])
def posts():
if request.method == 'POST':
PostTitle = request.form['PostTitle']
PostAuthor = request.form['PostAuthor']
PostContent = request.form['PostContent']
NewPost = BlogPost(title=PostTitle,content=PostContent,author=PostAuthor)
db.session.add(NewPost)
db.session.commit()
redirect('/posts')
else:
AllPost = BlogPost.query.order_by(BlogPost.id).all()
return render_template('post.html',posts=AllPost)
这是我的html代码:
<hr>
<h2>Create New Blog Post:</h2>
<form action='/posts' method='POST'>
Title: <input name='PostTitle' type='text' id='PostTitle'>
<br>
Author: <input name='PostAuthor' type='text' id='PostAuthor'>
<br>
Post: <input name='PostContent' type='text' id='PostContent'>
<br>
<input type='submit' value='Post'>
</form>
<hr>
,当我尝试运行服务器进行测试时,出现此错误: 浏览器(或代理)发送了该服务器无法理解的请求。 KeyError:“ PostTitle”
我的代码有什么问题,有人可以帮助我吗?