2

I am currently working on a flask_app. This is my project structure

├── pypi_org
│   ├── __pycache__
│   │   └── app.cpython-37.pyc
│   ├── app.py
│   ├── services
│   │   └── package_services.py
│   ├── static
│   │   ├── css
│   │   │   └── site.css
│   │   ├── html
│   │   └── js
│   ├── templates
│   │   ├── home
│   │   │   ├── about.html
│   │   │   └── index.html
│   │   ├── packages
│   │   │   └── details.html
│   │   └── shared
│   │       └── _layout.html
│   ├── tests
│   ├── viewmodels
│   └── views
│       ├── home_views.py
│       └── package_views.py
├── requirements-dev.txt
└── requirements.text

I have defined the blueprint in home_views.py

from flask import render_template
from flask import Blueprint
from pypi_org.services import package_services

blueprint = Blueprint('home', __name__, template_folder='templates')


@blueprint.route('/')
def index():
    test_packages = package_services.get_latest_packages()
    return render_template('home/index.html', packages=test_packages)


@blueprint.route('/about')
def about():
    return render_template('home/about.html')

Code for app.py is given below

from flask import Flask
from pypi_org.views import home_views, package_views

app = Flask(__name__)


def main():
    register_blueprints()
    app.run(debug=True)


def register_blueprints():
    app.register_blueprint(home_views.blueprint)
    app.register_blueprint(package_views.blueprint)


if __name__ == '__main__':
    main()

When I run the app.py I get the 404 error. I'm not sure what I am doing wrong. Everything looks right.

Can someone take a look?

sampippin
  • 123
  • 1
  • 2
  • 12
  • What happens if you comment out `app.register_blueprint(package_views.blueprint)`? – Dave W. Smith Jul 04 '19 at 21:43
  • Try putting your `html` files inside another `templates` folder e.g. `templates\home\templates\index.html`, see http://flask.pocoo.org/docs/1.0/blueprints/#templates – PGHE Jul 04 '19 at 22:21
  • I am using pycharm. If the cwd is `/Applications/PyCharm.app/Contents/bin` , then it works without any issues. not sure why this is the case – sampippin Jul 04 '19 at 22:42
  • @sampippin Did you finally figure out what it is? I have the same issue – Bogdan Mind Dec 07 '21 at 14:05

1 Answers1

0

I ran into the exact same issue within the same course.

If you right-click the app.py file and select run, Pycharm will create a run configuration, in which it will execute "app.run". It won't care about what you defined in your main(), and so the register_blueprints() will not be run!

The same run configuration will be created, if you create one yourself selecting Flask server. This would be again a bad idea.

To overcome this issue, create a run configuration where you select python. The same way as you would run any python script. This way Pycharm will execute "python app.py" (not "app.run") and it will run all lines in that file.

Lac
  • 71
  • 1
  • 5