Day 1 of 5
⏱ ~60 minutes
Flask in 5 Days — Day 1

Routes, Views, and Jinja2 Templates

Install Flask, define your first routes, return HTML templates with Jinja2, and understand the request/response objects.

Your First Flask App

Terminal
pip install flask
mkdir my-app && cd my-app
app.py
from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)

@app.route('/')
def index():
    name = request.args.get('name', 'World')
    return render_template('index.html', name=name)

@app.route('/hello/')
def hello(username):
    return f'Hello, {username}!'

@app.route('/submit', methods=['GET', 'POST'])
def submit():
    if request.method == 'POST':
        data = request.form.get('message')
        return redirect(url_for('index'))
    return render_template('form.html')

if __name__ == '__main__':
    app.run(debug=True)
templates/index.html


Flask App

  

Hello, {{ name }}!

{% if name != 'World' %}

Nice to meet you, {{ name }}.

{% endif %} {% for item in items %}
  • {{ item }}
  • {% endfor %} Home
    Finished this lesson?
    💡
    Use url_for('view_name') to generate URLs in templates and code. Never hardcode /submit. If the URL changes, url_for still works. Hardcoded URLs break silently.
    📝 Day 1 Exercise
    Build a Multi-Route App
    1. C
    2. r
    3. e
    4. a
    5. t
    6. e
    7. r
    8. o
    9. u
    10. t
    11. e
    12. s
    13. f
    14. o
    15. r
    16. /
    17. ,
    18. /
    19. a
    20. b
    21. o
    22. u
    23. t
    24. ,
    25. a
    26. n
    27. d
    28. /
    29. g
    30. r
    31. e
    32. e
    33. t
    34. /
    35. &
    36. l
    37. t
    38. ;
    39. n
    40. a
    41. m
    42. e
    43. &
    44. g
    45. t
    46. ;
    47. .
    48. C
    49. r
    50. e
    51. a
    52. t
    53. e
    54. a
    55. b
    56. a
    57. s
    58. e
    59. t
    60. e
    61. m
    62. p
    63. l
    64. a
    65. t
    66. e
    67. w
    68. i
    69. t
    70. h
    71. a
    72. n
    73. a
    74. v
    75. .
    76. E
    77. a
    78. c
    79. h
    80. p
    81. a
    82. g
    83. e
    84. s
    85. h
    86. o
    87. u
    88. l
    89. d
    90. e
    91. x
    92. t
    93. e
    94. n
    95. d
    96. i
    97. t
    98. .
    99. R
    100. e
    101. n
    102. d
    103. e
    104. r
    105. a
    106. l
    107. i
    108. s
    109. t
    110. o
    111. f
    112. i
    113. t
    114. e
    115. m
    116. s
    117. p
    118. a
    119. s
    120. s
    121. e
    122. d
    123. f
    124. r
    125. o
    126. m
    127. P
    128. y
    129. t
    130. h
    131. o
    132. n
    133. t
    134. o
    135. a
    136. J
    137. i
    138. n
    139. j
    140. a
    141. 2
    142. t
    143. e
    144. m
    145. p
    146. l
    147. a
    148. t
    149. e
    150. .

    Day 1 Summary

    • @app.route('/path') maps a URL to a Python function.
    • render_template('file.html', key=value) renders Jinja2 templates with context.
    • request.args = query params. request.form = POST form data.
    • url_for('function_name') generates URLs. Always use it instead of hardcoding.