Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask views in separate module

flaskr.py

# flaskr.py    
from flask import Flask


app = Flask(__name__)

import views


if __name__ == "__main__":
    app.run()

views.py

# views.py
from flaskr import app
from flask import render_template, g

@app.route('/')
def show_entries():
    entries = None
    return render_template('show_entries.html', entries=entries)

python3 flaskr.py

Can anyone tell me why this isn't working but if i move the whole app into a separate package it works flawless.

No errors, not nothing except a 404 like the views.py is ignored. I'm knew to Flask and i'm trying out different things to understand how it actually works.

Thanks!

like image 971
Viorel Avatar asked Dec 01 '13 12:12

Viorel


1 Answers

If you want to move views to other file you need to register blueprint:

flask.py

# flaskr.py    
from flask import Flask
from .views import my_view

app = Flask(__name__)
app.register_blueprint(my_view)

if __name__ == "__main__":
    app.run()

views.py

# views.py
from flaskr import app
from flask import render_template, g

my_view = Blueprint('my_view', __name__)

@app.route('/')
def show_entries():
    entries = None
    return render_template('show_entries.html', entries=entries)

Similar questions:

  • URL building with Flask and non-unique handler names
  • Using flask/blueprint for some static pages
like image 73
KiraLT Avatar answered Nov 15 '22 14:11

KiraLT