Tuesday, October 4, 2022

url_for method

 url_for to genreate urls at runtime.


static/css/styles.css


/* THESE STYLES ARE FOR THE TABLE IN SECTION 3: LECTURE 14 */

table {
    border-collapse: collapse;
    width: 35%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 4px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}

templates/table_data.html


<!-- HTML FOR SECTION 3: LECTURE 14 -->
<!-- ADDED CSS USING UR_FOR METHOD TO THIS HTML FILE IN LINE 10 -->


<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>movie durations</title>
        <link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
    </head>

    <body>
        <h2>This Collection Belongs to {{ name }} </h2>
        <!--the following url_for is ony for your understanding. dont include this in your projects-->
        <h3> Path to the css file: {{ url_for('static', filename='css/styles.css') }} </h3>

        <table>
            <tr>
                <th> movie </th>
                <th> length </th>
                <th> comments </th>
            </tr>

            {% for movie, length in movies.items() %}
                <tr>
                    <td> {{ movie }} </td>
                    <td> {{ length }} </td>
                        {% if length <= 2 %}
                            <td> short </td>
                        {% elif length >= 2 and length <=3 %}
                            <td> medium </td>
                        {% else %}
                            <td style="color: red">long</td>
                        {% endif %}
                </tr>
            {% endfor %}
        </table>
    </body>
</html>

No comments:

Post a Comment