我正在写博客,并向网站添加了add_article函数。
当我尝试添加文章时,在终端上我可以看到请求是POST且工作正常,但是我没有收到Flash消息或重定向到指定的url,并且文章未保存到MySQL数据库。
我尝试打印form.errors,但也没有显示任何内容。
from flask import Flask, render_template, flash, redirect, url_for, session, logging, request
from flask_mysqldb import MySQL
from wtforms import Form, StringField, TextAreaField, PasswordField, validators
from passlib.hash import sha256_crypt
from functools import wraps
class Article(Form):
title = StringField("Title",validators=[validators.Length(min=5, max=100)])
content = TextAreaField("Content", validators=[validators.Length(min=10)])
@app.route("/addarticle", methods = ["GET", "POST"])
def add_article():
form = Article(request.form)
if request.method == ["POST"] and form.validate():
title = form.title.data
content = form.content.data
cursor = MySQL.connection.cursor
inquiry = "INSERT INTO articles(title, author, content) VALUES(%s,%s,%s)"
cursor.execute(inquiry,(title, session["username"], content))
mysql.connection.commit()
cursor.close()
flash("Article added successfuly.","success")
return redirect(url_for("dashboard"))
return render_template("addarticle.html", form = form)