Python Forum
Need help in Code for Writing View Functions in Flask - Python Web Framework
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help in Code for Writing View Functions in Flask - Python Web Framework
#3
You have skipped the second task in view function hello_user.
It shall return a randomly chosen quote in h3 tag.
So to help with this view function can look like this.
from flask import Flask
from random import choice

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!!! I've run my first Flask application."

@app.route("/hello/<username>/")
def hello_user(username):
    quotes = [
        "Only two things are infinite, the universe and human stupidity, and I am not sure about the former.",
        "Give me six hours to chop down a tree and I will spend the first four sharpening the axe.",
        "Tell me and I forget. Teach me and I remember. Involve me and I learn.",
        "Listen to many, speak to a few.",
        "Only when the tide goes out do you discover who has been swimming naked.",
    ]
    return f"<h2>Hello {username}</h2><h3>{choice(quotes)}</h3>"

if __name__ == '__main__':
   app.run(host='0.0.0.0', port=8000, debug=True)
Hint for last view function.
Move quotes list so it's before return render_template.
In test.html use jinja to displaying quotes.
Eg.
<body>
{% for list_element in name %}
  <ul>
    <li>{{ list_element }}</li>
  </ul>
{% endfor %}
</body>
Reply


Messages In This Thread
RE: Need help in Code for Writing View Functions in Flask - Python Web Framework - by snippsat - May-22-2020, 03:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I need help writing this code for string properties and methods hannah71605 4 3,143 Mar-22-2021, 12:36 PM
Last Post: menator01
  How Do I Increment My View Counter in Flask? martin28 1 5,840 Jan-11-2019, 07:48 PM
Last Post: nilamo
  HELP - Writing code returning True or False Kokuzuma 2 2,804 Nov-01-2018, 03:37 AM
Last Post: Kokuzuma
  Speed of progress of writing code Johno 2 2,905 Jan-19-2018, 01:56 AM
Last Post: dwiga
  Problem writing code with my pseudocode MattWilk97 1 2,898 Aug-29-2017, 01:54 AM
Last Post: ichabod801
  Need Help with writing this code!? r6lay 7 5,938 Feb-13-2017, 10:03 AM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020