pyCthon
pyCthon

Reputation: 12341

Split Python Flask-Menu app into multiple files

As an extension of this question Split Python Flask app into multiple files

I want to use Flask-Menu and have each separate menu page in it's own Python file.

Here is my main site main.py file this has the /first menu item

from flask import Flask, render_template, Blueprint, abort
from flask_wtf import Form
from flask.ext import menu

from wtforms import HiddenField

from second import bp_second

class EmptyForm(Form):
    hidden_field = HiddenField('You cannot see this', description='Nope')

def create_app(configfile=None):
    app = Flask(__name__)
    app.register_blueprint(bp_second)
    menu.Menu(app=app)

    @app.route('/')
    @menu.register_menu(app, '.', 'Home')
    def index():
        form = EmptyForm()
        form.validate_on_submit()
        return render_template('index.html', form=form)

    @app.route('/first')
    @menu.register_menu(app, '.first', 'First', order=0)
    def first():
        form = EmptyForm()
        form.validate_on_submit()
        return render_template('index.html', form=form)

    return app

if __name__ == '__main__':
    create_app().run(debug=True)

I have a menu item called second.py here this has the /second menu item

from flask import Blueprint, render_template
from flask_wtf import Form
from flask.ext import menu

from wtforms import TextField

class TextForm(Form):
    text = TextField(u'text', [validators.Length(min=2, max=5, message="my item")])

bp_second = Blueprint('second', __name__, url_prefix='/second')

@bp_second.route("/second")
@menu.register_menu(bp_second, '.second', 'Second', order=1)
def second():
    form = TickerForm()
    form.validate_on_submit() #to get error messages to the browser
    return render_template('index.html', form=form)

How ever when I click the menu item for /second I get a "GET /meta HTTP/1.1" 404 - message. The / and /first menu items work

Upvotes: 1

Views: 1089

Answers (1)

VKolev
VKolev

Reputation: 825

I suspect, that your route is wrong. As I read it your route for second is /second/second so your menu entry should be

@menu.register_menu(bp_second, '.second.second', 'Second', order=1)

You can check the Flask-Menu documentation about Blueprints Support in Flask-Menu

Upvotes: 1

Related Questions