[英]POST method is not working on Flask application - Error 404
我正在嘗試構建一個簡單的 Flask 應用程序,它將運行一個連接到 Postgres 數據庫的 web 應用程序。
但是,當我運行代碼並單擊提交按鈕時,POST 方法不起作用,因此它確實按指定返回 {{url_for('success]}}。我嘗試添加和刪除幾個組件。我有 methods= ['GET', 'POST'] 已經。
應用程序.py:
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql import func
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='postgresql://postgres:postgres@localhost/Jeopardy'
db=SQLAlchemy(app)
class Data(db.Model):
__tablename__="allc"
number = db.Column(db.Integer, primary_key=True)
category = db.Column(db.String())
question = db.Column(db.String())
answer = db.Column(db.String())
def __init__(self,number,category,question,answer):
self.number = number
self.category = category
self.question = question
self.answer = answer
@app.route("/")
def index():
return render_template("index.html")
@app.route("/success", methods=['POST','GET'])
def success():
if request.method=='POST':
category=request.form['category']
question=request.form['question']
answer=request.form['answer']
number=request.form['number']
print(email, height)
if db.session.query(Data).filter(Data.number == number).count()== 0:
data=Data(number,category,question,answer)
db.session.add(data)
db.session.commit()
return render_template("success.html")
if __name__ == '__main__':
app.debug=True
app.run()
索引.html:
<html lang="en">
<title>Jeopardy</title>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device=width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="../static/style.css" rel="stylesheet" >
</head>
<body>
<div class="="container">
<img src="../static/logo.png" alt="Jeopardy" class ="logo">
<!-- @todo - message-->
<form action = "s{{url_for('success')}}" methods="POST">
<div class = " form-group">
<h3>Jeopardy Question</h3>
<input
type = "number"
name = "Index"
placeholder= "Type number" />
<input
type = "text"
name = "Question"
placeholder= "Type the Jeopardy question here" />
<input
type = "text"
name = "Answer"
placeholder= "Type the Jeopardy Answer here"/>
<button type = "submit"> Submit</button>
</form>
</div>
</body>
</html>
運行代碼時,我的應用程序成功呈現,但在提交數字時,服務器未注冊輸入。 單獨加載成功頁面時,它加載。 在終端中,我看到: "POST /success HTTP/1.1" 404 -
你的 html 打錯了
它應該是method="post"
而不是methods="post"
編輯:
另一個錯別字
action = "s{{url_for('success')}}"
刪除“s”
這是錯字:
<form action = "s{{url_for('success')}}" methods="POST">
將其更改為:
<form action = "{{url_for('success')}}" method="POST">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.