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
#1
We had a below task, we that tried to write code in what ever best possible way we can. But we are not able to pass the test as seems there is some issue in the code. hence need help to correct the code. request if some one can help us here it will be great for us.


from flask import Flask

## Define a flask application name 'app' below
app = Flask(__name__)

## Define below a view function 'hello', which displays the message 
## "Hello World!!! I've run my first Flask application."
## The view function 'hello' should be mapped to URL '/' .
@app.route("/")
def hello():
   return "Hello World!!! I've run my first Flask application."

## Define below a view function 'hello_user', which takes 'username' as an argument 
## and returns the html string containing a 'h2' header  "Hello <username>"
## After displaying the hello message, the html string must also display one quote, 
## randomly chosen from the provided list `quotes` 
# Before displaying the quote, the html string must contain the 'h3' header 'Quote of the Day for You' 
## The view function 'hello_user' should be mapped to URL '/hello/<username>/' .
## Use the below list 'quotes' in 'hello_user'  function
## 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."
##    ]
@app.route("/hello/<username>/")
def hello_user(username):
 return "Hello " + username + "Quote of the Day for You"

## Define below a view function 'display_quotes', which returns an html string 
## that displays all the quotes present in 'quotes' list in a unordered list.
## Before displaying 'quotes' as an unordered list, the html string must also include a 'h1' header "Famous Quotes".
## The view function 'display_quotes' should be mapped to URL '/quotes/' .
## Use the below list 'quotes' in 'display_quotes'  function
## 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."
##    ]
@app.route("/quotes/")
def display_quotes():
   return render_template( 'test.html',name=display_quotes)
      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."]
      randomNumber = randint(0,len(quotes)-1)
      quote = quotes[randomNumber]

## Write the required code below which runs flask applictaion 'app' defined above
## on host 0.0.0.0 and port 8000  
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)
Reply
#2
you can look here: https://flask.palletsprojects.com/en/1.1...ial/views/
and in Miguel Grinberg's Flask tutorial here:
https://blog.miguelgrinberg.com/post/the...nd-avatars
Reply
#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


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