简体   繁体   中英

Flask blueprint url not found

I'm trying to create a flask app using blueprints, so I have this structure:

myapp/
    __init__.py
    static/
    templates/
        site/
            index.html
            register.html
        layout.html
    views/
        __init__.py
        site.py
    models.py

__init__.py

from flask import Flask
from .views.site import site

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

views/site.py

from flask import Blueprint, render_template

site = Blueprint('site', __name__)

site.route('/')
def index():
    return render_template('site/index.html')

site.route('/register/')
def register():
    return render_template('site/register.html')

When I run the app, and try to go to any of the routes I have, the only thing I gest is a "404 Not Found" and I don't really know what I'm doing wrong, because I'm doing exactly what this book says: Explore Flask - Blueprints

Thanks in advance.

You have to prepend @ to site.route like the following.

from flask import Blueprint, render_template

site = Blueprint('site', __name__)

@site.route('/')
def index():
return render_template('site/index.html')

@site.route('/register/')
def register():
    return render_template('site/register.html')

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