Я новичок в Flask. Моя домашняя страница - index1.html. Я добавил панель навигации со ссылками на другие HTML-страницы. Как мне это сделать?
nav>
<button type = "button" id = "nav-toggle" onclick = "$('nav ul').toggle()">☰MENU</button>
<ul>
<li class = "active"><a href = "index1.html" class = "nav_tab" id = "_a">Overview</a></li>
<li><a href = "search.html" class = "nav_tab" id = "_b">Search</a></li>
</ul>
</nav>
Страницы html находятся в папке шаблона. Ссылка «обзор» должна указывать на домашнюю страницу (index1.html), а «поиск» - на страницу search.html. Как я могу добиться этого во флаконе? Мой routes.py выглядит так:
from flask import render_template
from tomvar import app
@app.route('/')
@app.route('/index')
def index():
return render_template('index1.html')






Те HTML-страницы, которые находятся в вашей папке шаблонов, должны находиться за каким-то маршрутом в вашем routes.py, поэтому вам нужно просто определить маршруты в своем HTML-теге href, примерно так. Щелкнув по примеру, вы перейдете на /search, за которым для вас откроется страница search.html.
<li class = "active"><a href = "/search">Example</a></li>
Second Option
Или есть другое решение для этого, вы можете использовать url_for, генерирующий URL-адреса для маршрутов, определенных в вашем приложении.
routes.py:
from flask import Flask, request, url_for, redirect, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/index2', methods=['GET', 'POST'])
def index_func():
if request.method == 'POST':
# do stuff when the form is submitted
# redirect to end the POST handling
# the redirect can be to the same route or somewhere else
return redirect(url_for('index'))
# show the form, it wasn't submitted
return render_template('index2.html')
шаблоны / index.html:
<!doctype html>
<html>
<body>
<p><a href = "{{ url_for('index_func') }}">Check out</a></p>
</body>
</html>
Используйте
url_for