A
A
anton_zaboev2021-11-30 02:12:35
Flask
anton_zaboev, 2021-11-30 02:12:35

How to output url_for in template?

Goodnight! Started learning Flask and can't figure out url_for. Initially, I kept the page name and link in the dictionary and everything worked fine, then I found out that it’s more correct to do urls through url_for. But I don’t know how to display it correctly in the template through the for loop, and in general the error is related to how I call it in the template?61a55dacf2319402398405.jpeg

from flask import Flask, request, render_template, url_for

app = Flask(__name__)

# menu = {'Главная': '/',
#         'Задача 1': '/lesson1',
#         'Задача 2': '/lesson2',
#         'Задача 3': '/lesson3',
#         'Задача 4': '/lesson4'}
menu = ['Главная', 'Задача 1', 'Задача 2', 'Задача 3', 'Задача 4']


@app.route('/')
def hello_world():
    print(url_for('index'))
    return render_template('index.html', title="Для проверки домашнего задания авторизуйтесь!!!", menu=menu)


@app.route('/lesson1')
def lesson1():
    print(url_for('lesson1'))
    return render_template('lesson1.html', title="Задача 1", menu=menu)


@app.route('/lesson2')
def lesson2():
    print(url_for('lesson2'))
    return render_template('lesson2.html', title="Задача 2", menu=menu)


@app.route('/lesson3')
def lesson3():
    print(url_for('lesson3'))
    return render_template('lesson3.html', title="Задача 3", menu=menu)


@app.route('/lesson4')
def lesson4():
    print(url_for('lesson4'))
    return render_template('lesson4.html', title="Задача 4", menu=menu)


if __name__ == '__main__':
    app.jinja_env.auto_reload = True
    app.config['TEMPLATES_AUTO_RELOAD'] = True
    app.run(debug=True)

# with app.test_request_context():
# ...     print(url_for('index'))
# ...     print(url_for('login'))
# ...     # переменной `next` нет в маршруте `/login`
# ...     print(url_for('login', next='then'))
# ...     print(url_for('profile', username='admin'))

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
          integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    <style>
        .logo-image {
            width: 145px;
            height: 23px
        }
    </style>
    {% block title -%}
        {% if title %}
            <title>{{ title }}</title>
        {% else %}
            <title>Title</title>
        {% endif %}
    {% endblock %}
</head>
<body>
{% block content -%}
<div class="container">
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container-fluid">
            
               
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarText"
                    aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

            <div class="collapse navbar-collapse" id="navbarText">
                {%- block mainmenu -%}
                    <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                        {% for title in menu %}
                            <li class="nav-item">
                                <a class="nav-link active" aria-current="page" href="{{ url_for() }}">{{ title }}</a>
                            </li>
                        {% endfor %}
                    </ul>
                {% endblock mainmenu -%}
                <form class="d-flex">
                    <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
                    <button class="btn btn-outline-light" type="submit">Search</button>
                </form>
            </div>
        </div>
    </nav>

    {% if title -%}
        <h1 class="mt-5 mb-5">{{ title }}</h1>
    {% else %}
        <h1 class="mt-5 mb-5">Для проверки домашнего задания авторизуйтесь!!!</h1>
    {% endif -%}
</div>
{% endblock -%}
</body>
</html>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question