Getting ""Not Found The requested URL was not found on the server" when Flask app server is placed in folder with other files needed for application

lna21

For the program I am making, I need to be able to send POST requests to the local server in order to control the game. I am using Flask to run all of this.

The app.py contains the Flask code that sets up the server and receives requests. When the app.py is outside any folders and simply in my Desktop it works just fine. When in the folders, the organization of which is shown below, the page does not work, and gives the 404 error. And when I go to the localhost address it tells me the URL cannot be found.

I outline the problem in more detail below. I included the folder organization, errors I receive in terminal and the server, how I run the code, and what code I have written in each file. I am not sure what is causing the problem.

  1. My files are organized as follows:
game/
  game.py
  app/
    __init__.py
    app.py
  1. When I run the program in the terminal, I cd into the app folder, and I enter the following commands in the command line:

    • export FLASK_APP=game.py
    • flask run
  2. This is what is printed in the terminal when I go to the localhost which the app.py is supposed to create. I bolded where it says that it does not exist:

     * Debug mode: off
     * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
    127.0.0.1 - - [26/Dec/2019 23:29:28] "GET / HTTP/1.1" 404 -
    
  3. On the localhost webpage, I get the following error:

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

  4. File content:

    • game.py
    from app import app
    
    • __init__.py
    from flask import Flask
    
    app = Flask(__name__)
    
    • app.py
    from flask import Flask, request 
    
    app = Flask(__name__) #create the Flask app
    
    @app.route('/', methods=['POST','GET'])
    def form():
        if request.method == 'POST':
            concentrated = request.form.get('concentrated')
            return '''The person is concentrated? {}'''.format(concentrated)
    
         return '''<form method="POST">
            Concentrated <input type="text" name="concentrated">
            <input type="submit">
            </form>'''
    
    
    if __name__ == '__main__':
        app.run(debug=True, port=5000)
    

I looked at this question, however it does not seem to help in my situation: Why does localhost:5000 not work in Flask?

aman kumar

In app/__init__.py:

from flask import Flask

app = Flask(__name__)

from .app import *

In app.py:

from flask import Flask, request 

from app import app

@app.route('/', methods=['POST','GET'])
def form():
  if request.method == 'POST':
      concentrated = request.form.get('concentrated')
      return '''The person is concentrated? {}'''.format(concentrated)

     return '''<form method="POST">
      Concentrated <input type="text" name="concentrated">
      <input type="submit">
      </form>'''

In game.py, it should be:

from app import app

if __name__ == '__main__':
    app.run(debug=True, port=5000)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related