繁体   English   中英

Python Flask validate_on_submit() https 错误

[英]Python Flask validate_on_submit() https error

帮助我在这里被困了将近 3 天,现在我发布了我的疑问,听到我的代码显示 405 错误

@app.route("/category/add")
def add_category():
    form = AddCategory()
    if form.validate_on_submit():
        new_category = Categories(
            title=form.name.data,
            description=form.description.data,
            review=form.review.data,
            img_url=form.img_url.data,
        )
        db.session.add(new_category)
        db.session.commit()
        return render_template("index.html")
    return render_template("add_category.html", form=form)
class AddCategory(FlaskForm):
    name = StringField(label='Category name', validators=[DataRequired()])
    description = StringField(label='Description', validators=[DataRequired()])
    review = StringField(label='Review', validators=[DataRequired()])
    img_url = StringField(label='Image Url ', validators=[DataRequired(), URL()])
    submit = SubmitField(label='Add')

它运行完美,直到条件语句

{% extends 'bootstrap/base.html' %}
{% import 'bootstrap/wtf.html' as wtf%}

{% block styles %}
  {{ super() }}
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Nunito+Sans:300,400,700">
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Poppins:300,400,700">
  <link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
{% endblock %}

{% block title %}Add Category{% endblock %}

{% block content %}
<div class="content">
    <h1 class="heading">Add a Category</h1>
    {{wtf.quick_form(form, novalidate=True)}}

</div>
{% endblock %}

add_category.html

我很确定 Mandraenke 的评论是正确的。 现在,您的路由只允许 GET 方法,通过 form.validate_on_submit() 假定 POST 方法。 所以你需要明确指定它。 像那样:

@app.route("/category/add", methods=['GET', 'POST'])
def add_category():
    form = AddCategory()
    if form.validate_on_submit():
        new_category = Categories(
            title=form.name.data,
            description=form.description.data,
            review=form.review.data,
            img_url=form.img_url.data,
        )
        db.session.add(new_category)
        db.session.commit()
        return render_template("index.html")
    return render_template("add_category.html", form=form)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM