简体   繁体   中英

FLASK - The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again

I am running this site using Flask. When I run the script, the initial site (index.html) appears, and everything seems fine. However, when I click submit at the bottom of the page, I encounter this error "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again."

Could you please help me find the problem? The code for the main app is below.

 from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

ENV = 'dev'
   
if ENV =='dev':
    app.debug = True
    app.config['SQLALCHEMY_DATABASEU_URI']   = 'postgresql://postgres:postgres@localhost/Jeopardy'
else:
    app.debug = False
    app.config['SQLALCHEMY_DATABASEU_URI']   = ''

#sapp.config.from_object(os.environ['APP_SETTINGS'])
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False


db=SQLAlchemy(app)

class Jeo(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,category,question,answer,number):
        self.category = category
        self.question = question
        self.answer = answer
        self.number = number

@app.route('/')
def index():
    return render_template("index.html")
@app.route("/add")
def add():
    category=request.args.get('category')
    question=request.args.get('question')
    answer=request.args.get('answer')
    number=request.args.get('number')
    try:
        allc=Jeo(
            category = category,
            question = question,
            answer = answer,
            number = number
        )
        db.session.add(allc)
        db.session.commit()
        return " added. "
    except Exception as e:
        return(str(e))


@app.route("/add/form",methods=['GET', 'POST'])
def add_form():
    if request.method == 'POST':
        category=request.get('category')
        question=request.get('question')
        answer=request.get('answer')
        number=request.get('number')
    
    try:
            allc=Jeo(
            category = category,
            question = question,
            answer = answer,
            number = number
            )
            db.session.add(allc)
            db.session.commit()
            return ("added. ")
    except Exception as e:
            return(str(e))
    return render_template('index.html')
if __name__ == '__main__':
    app.run()

And the code index.html code is:

<html lang="en">
<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 rel="stylesheet" href="../static/style.css">
    <title>Jeopardy</title>
</head>
<body>
    <div class="="container">
        <img src="../static/logo.png" alt="Jeopardy" class ="logo">
        <!-- @todo - message-->
        <form action = "/submit" methods="POST">
            <div class =  " form-group">
            <h3>Jeopardy Question</h3>
                <input
                    type = "text"
                    name = "Question"
                    placeholder= "Type the Jeopardy question here" 
                    />
            </div>
            <div class =  " form-group">
                <h3>Jeopardy Answer</h3>
                    <input
                        type = "text"
                        name = "Answer"
                        placeholder= "Type the Jeopardy Answer here" 
                        />

                </div>
            
        </form>
    </div>
</body>
</html>

The problem exists here...The place where you have to write form action="add" you have written it as action="submit"

<form action = "/add" methods="POST">
            <div class =  " form-group">
            <h3>Jeopardy Question</h3>
                <input
                    type = "text"
                    name = "Question"
                    placeholder= "Type the Jeopardy question here" 
                    />
            </div>
            <div class =  " form-group">
                <h3>Jeopardy Answer</h3>
                    <input
                        type = "text"
                        name = "Answer"
                        placeholder= "Type the Jeopardy Answer here" 
                        />

                </div>
  
        </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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