The same url_prefix for two illustrations

I want to implement a simple site layout:

/ should display home.html

/one, /two, /threeShould display one.html, two.html, three.htmlrespectively

So far I have come up with the following code:

main_page = Blueprint('main', __name__)
category_page = Blueprint('category', __name__)


@main_page.route("/")
def home():
    return render_template('home.html')


@category_page.route('/<category>')
def show(category):
    return render_template('{}.html'.format(category))

app = Flask(__name__)
app.register_blueprint(main_page, url_prefix='/')
app.register_blueprint(category_page, url_prefix='/categories')

That way I can route categoriesto /categories/<category>. How can I direct them only to /<category>, while preserving the home.htmlrelated /? Appreciate your help

I tried two approaches:

  • Setting url_prefix='/'for both drawings => the second does not work.

  • Instead, main_pageuse only app.route('/')for rendering home.html. This also does not work when mixing with the category_pageplan.

+4
source share
1 answer

url_prefix :

@main_page.route("/")
def home():
    return render_template('home.html')
app.register_blueprint(main_page, url_prefix='/')

@category_page.route('/')
def show(category):
    return render_template('{}.html'.format(category))        
app.register_blueprint(category_page, url_prefix='/<category>')

( , , , .)

+3

Source: https://habr.com/ru/post/1687962/


All Articles