0

I am trying to organize my flask project, but there's something wrong. I have this directory:

app/ 
    __init__.py
    views/
        pages.py

On my __init__.py file I've imported the pages object and registered it as a blue print.

This is the code on my pages.py file.

from flask import Blueprint, render_template

pages = Blueprint('pages', __name__) #no prefix

@pages.route('/')
def index():
    return '<h1>in index.html</h1>'

@pages.route('/home')
def home():
    return '<h1>in home.html</h1>'

If I run the flask app, open the browser, and go to localhost:5000, I will see the headline 'in index.html'.
But if I go to localhost:5000/home, I will get the message 404 Not Found message.

Does anyone know the reason for this behavior?

Henrik
  • 2,771
  • 1
  • 23
  • 33

2 Answers2

2

Ok, first the folder structure:

app/
  __init__.py
  main.py

  views/
  __init__.py
  test.py

Contents of main.py:

from flask import Flask
from views.test import pages

app = Flask(__name__)
app.register_blueprint(pages) <-- blueprint registration

Contents of test.py:

from flask import Blueprint

pages = Blueprint('pages', __name__) #no prefix


@pages.route('/')
def index():
    return '<h1>in index.html</h1>'


@pages.route('/home')
def home():
    return '<h1>in home.html</h1>'

I believe the register_blueprint was the only thing missing.

Fábio Correia
  • 595
  • 7
  • 16
  • 1
    Thank you for the devotion. but as i said, i did register the blue print. When I'll find the answer I could update you if you're interested. – Daniel Arad Jun 12 '18 at 20:20
  • I don't know if you posted all your folder structure and code with "main" flask, if you could update it, I would be glad to continue helping you. – Fábio Correia Jun 12 '18 at 22:01
  • 2
    No changes made, just turn all off and restart computer. Fixed. Thank you. – Daniel Arad Jun 13 '18 at 18:28
-2

When stuff like that happen, just turn off everything, reset your computer.

Sometimes the bug is not yours.