幫助我在這里被困了將近 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
uj5u.com熱心網友回復:
我很確定 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/316760.html
標籤:Python 烧瓶 烧瓶wtforms http-status-code-405
