简体   繁体   English

POST 方法不适用于 Flask 应用程序 - 错误 404

[英]POST method is not working on Flask application - Error 404

I am trying to build a simple Flask application that will run a web app that connects to a Postgres database.我正在尝试构建一个简单的 Flask 应用程序,它将运行一个连接到 Postgres 数据库的 web 应用程序。

However, when I run the code and click the submit button, the POST method does not work, so it does return the {{url_for('success]}} as specified. I have tried adding and removing several components. I have methods=['GET', 'POST'] already.但是,当我运行代码并单击提交按钮时,POST 方法不起作用,因此它确实按指定返回 {{url_for('success]}}。我尝试添加和删除几个组件。我有 methods= ['GET', 'POST'] 已经。

app.py:应用程序.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()

index.html:索引.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>

While running the code my app renders successfully, but when submitting a number the server does not register the input.运行代码时,我的应用程序成功呈现,但在提交数字时,服务器未注册输入。 When loading the success page separately, it loads.单独加载成功页面时,它加载。 In the terminal, I see: "POST /success HTTP/1.1" 404 -在终端中,我看到: "POST /success HTTP/1.1" 404 -

You have a typo in your html你的 html 打错了

It should be method="post" and not methods="post"它应该是method="post"而不是methods="post"

EDIT:编辑:

Another typo in另一个错别字

action = "s{{url_for('success')}}"

Remove the "s"删除“s”

here is typo:这是错字:

<form action = "s{{url_for('success')}}" methods="POST">

change it to:将其更改为:

<form action = "{{url_for('success')}}" method="POST">

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

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