简体   繁体   中英

How to use subfolders with Flask Blueprints?

So I'm fairly new to Flask, but now I'm trying to use it for a project in a team, and, as all good projects should, we wanted to have a good folder structure to keep everything nice, neat and organised however we keep running into problems with using subfolders within the template folder.

This is the way we want our structure to be:

  • src
    • templates
      • About
        • about.html
        • about.css
        • about.js
      • Index
        • index.html
        • index.css
        • index.js

Where About would be a classic about us page, and profile a classic profile page. However we're really confused on how to actually route this - for example we don't understand why something like this doesn't work...

from flask import Blueprint, render_template, request, flash, jsonify
import json

views = Blueprint('views', __name__)

@views.route('/', methods=["GET", "POST"])
def index():
    return render_template('Index/index.html')

@views.route('/about', methods=["GET", "POST"])
def about():
    return render_template('About/about.html')

We're stumped here and would really appreciate any help that you could give!

"something does not work" is not a very helpful error message. You always should include the complete traceback or the observed behavior in the browser.

At least you provided a code sample, and there I see you do not use blueprints properly. At least from what I read, you want to have separate blueprints for the different areas in your application.

I usually use one blueprint for authentication, one for business case a, one for business b, ...

For that, you need to instantiate one blueprint for each area.

I recommend to read the following tutorial completely, but if you are in a hurry, just read the chapter about blueprints:

https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xv-a-better-application-structure

As you see, this is chapter 15, There is absolutely no need to begin with a complicated directory structure from the very beginning, especially, as you stated. you are a Flask beginner.

I like to follow these wise words "Make It Work Make It Right Make It Fast"

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