简体   繁体   中英

How to return both html template and string in Python Flask

I need to pass a string as well as to return a template during execution.

I have tried passing variable to html page, but the test case is failing because it counts tags in output. My test condition is to pass template called quotes.html and also to return a string.(List shown in code) (May or maynot in quote.html>

My Code:

 @app.route("/quotes/")
 def display_quotes():
return render_template("quotes.html",ans="<h1>Famous Quotes</h1>"+"""<ul>
         <li>Only two things are infinite, the universe and human stupidity, and I am not sure about 
          the former.</li>
         <li>Give me six hours to chop down a tree and I will spend the first four sharpening the 
         axe.</li>
         <li>Tell me and I forget. Teach me and I remember. Involve me and I learn.</li>
         <li>Listen to many, speak to a few.</li>
         <li>Only when the tide goes out do you discover who has been swimming naked.</li>
 </ul>""")

In quotes.html:

{{ans|safe}}

Error Message:

     def test_response_nquotes(self):
        response = self.client.get('/quotes/')
        print(response.data)
        nquotes = sum([ 1 for quote in self.quotes if quote in response.data ])
     >       assert nquotes == 5
     E       AssertionError: assert 3 == 5
     helloapp/tests.py:84: AssertionError
     ----------------------------- Captured stdout call -----------------------------
    b'<h1>Famous Quotes</h1>\n  <ul>\n  <li>Only two things are infinite, the universe and human stupidity, and I am not sure about the former.</li>\n  <li>Give me six hours to chop down a tree and I will spend the first four sharpening the axe.</li>\n  <li>Tell me and I forget. Teach me and I remember. Involve me and I learn.</li>\n  <li>Listen to many, speak to a few.</li>\n  <li>Only when the tide goes out do you discover who has been swimming naked.</li>\n  </ul>'
    _______________ TestDisplayQuotesView.test_response_valid_quotes _______________
    self = <tests.TestDisplayQuotesView testMethod=test_response_valid_quotes>
        def test_response_valid_quotes(self):
            response = self.client.get('/quotes/')
    >       assert all([ quote in response.data for quote in self.quotes ])
    E       AssertionError: assert False
    E        +  where False = all([False, True, True, True, False])
    helloapp/tests.py:88: AssertionError

Is there any way to return both html template and string in Python Flask?

Or any feasible solution to pass the test case?

from flask import Flask,render_template
import random
app=Flask(__name__)
@app.route('/')
## Define a flask application name 'app' below



@app.route('/')
def hello():
  return render_template("index.html")




@app.route('/hello/<username>/')
def hello_user(username):
  return render_template("hello_user.html",username=username)



@app.route('/quotes/')
def display_quotes():
  hi_quotes="Famous Quotes"
  quotes = [
               "Only two things are infinite, the universe and human stupidity, and I'm 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's been swimming naked."
            ]

  return render_template("quotes.html",hi_quotes=hi_quotes,quotes=quotes)
if __name__=='__main__':
  app.run(host='0.0.0.0',port=8000)

You have to add these lines of code in quotes.html file.
<h1>{{hi_quotes}}</h1>
{% for q in quotes %}
{{q}}
{% endfor %}


**if need any help just follow  me or connect me on +91 9023315532**

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM