简体   繁体   中英

python flask 404 not found

I am new to flask development, and can't the following application work. I wish to declare the Flask app in run.py, and import it in the other packages such as myapp below. I design it this way is because there are more than one independent applications/packages. I get 404 when trying to access the url ".../hello". Where am I wrong? Thanks!

The project layout is look like this:

├── myapp
│   ├── __init__.py
│   └── myapp.py
└── run.py

myapp/__init__.py contains:

from flask import Flask
my_app = Flask(__name__)
from . import myapp

myapp/myapp.py contains:

from run import app
@app.route('/hello')
def show_conf2():
    return "Hello, world!"

The run.py contains:

from flask import Flask
from gevent.pywsgi import WSGIServer
import myapp
@app.route('/')
def index():
    return 'Home route.'
app = Flask(__name__)
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()

You have declared two instances of Flask , one in __init__.py and another in run.py .

Flask official documentation has a tutorial on how to breakdown a larger application in multiple modules .

I have updated your code, filenames and folder structure.

Folder structure:

├── application
│   ├── __init__.py
│   └── views.py
├── requirements.txt
└── run_application.py

run_application.py :

from flask import Flask
from gevent.pywsgi import WSGIServer
from application import app

http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()

application/__init__.py :

from flask import Flask
app = Flask(__name__)

import application.views

application/views.py :

from application import app

@app.route('/')
def index():
    return 'Home route.'

@app.route('/hello')
def hello():
    return 'Hello World!'

requirements.txt :

Click==7.0
Flask==1.0.2
gevent==1.4.0
greenlet==0.4.15
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
Werkzeug==0.15.2

Run command:

python run_application.py

Output :

  1. Home route http://localhost:5000/

本国路线

  1. hello route http://localhost:5000/hello

你好路线

Update

According to Flask documentation on Blueprints ,

Flask uses a concept of blueprints for making application components and supporting common patterns within an application or across applications.

Updated the code to facilitate blueprints .

Updated Directory Structure:

├── application1
│   ├── __init__.py
│   └── routes.py
├── application2
│   ├── __init__.py
│   └── routes.py
├── apps.py
└── requirements.txt

apps.py :

from flask import Flask
from gevent.pywsgi import WSGIServer
from application1.routes import application1_blueprint
from application2.routes import application2_blueprint


app = Flask(__name__)
app.register_blueprint(application1_blueprint)
app.register_blueprint(application2_blueprint)    

http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()

application1/__init__.py : blank file

application1/routes.py :

from flask import Blueprint

application1_blueprint = Blueprint('application1', __name__)

@application1_blueprint.route('/app1')
def application1_index():
    return 'Home route for application1'

@application1_blueprint.route('/hello1')
def hello1():
    return 'Hello World from application1!'

application2/__init__.py : blank file

application2/routes.py :

from flask import Blueprint

application2_blueprint = Blueprint('application2', __name__)

@application2_blueprint.route('/app2')
def application2_index():
    return 'Home route for application2'

@application2_blueprint.route('/hello2')
def hello2():
    return 'Hello World from application2!'

Output:

/app1 route from application1 :

应用程序1视图

/app2 route from application2 :

应用2视图

In your code, I guess you execute the run.py in your flask application. In this case your application does NOT define any routing rule. Just modify and put your code into single file and it will be workable

from flask import Flask
from gevent.pywsgi import WSGIServer
#import myapp

app = Flask(__name__)

@app.route('/hello')
def show_conf2():
    return "Hello, world!"

http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()

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