嗨,我编写了一个博客脚本,但收到 400 Bad Request 错误。 Python代码:
@app.route('/panel',methods=['GET','POST'])
def panel():
if 'user_id' in session:
if request.method == "POST":
title = request.form['title']
context = request.form['text']
img = request.files['img']
author = request.form['author']
new = Posts(title=title,context=context,image=img.read(),author=author)
db.session.add(new)
db.session.commit()
return redirect(url_for('blog'))
else:
return redirect(url_for("admin"))
return render_template('panel.html')
HTML 代码:
<form action="" method="POST" class="form">
<ul>
<li>
<label for="title">Başlık</label>
<input name="title" id="title"/>
</li>
<li>
<label for="text">İçerik</label>
<input name="text" id="text">
</li>
<li>
<label for="img">Fotoğraf</label>
<input name="img" id="img" type="file" />
</li>
<li>
<label for="author">Yazar</label>
<input name="author" id="author"/>
</li>
<li>
<button type="submit">PAYLAS</button>
</li>
</ul>
</form>
我以前从未收到过这样的错误,我不知道为什么。我猜表格可能有错误。
请帮帮我
答案 0 :(得分:1)
以 Html 形式更改此行。它应该可以工作。
<form action="{{ url_for('panel') }}" method="post" class="form">
答案 1 :(得分:0)
如果您尝试上传文件,您的表单标签应包含属性 enctype="multipart/form-data"。例子:
<form action="upload.php" method="post" enctype="multipart/form-data">