简体   繁体   English

如何在 Python Flask 中同时返回 html 模板和字符串

[英]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.我曾尝试将变量传递给 html 页面,但测试用例失败了,因为它计算了输出中的标签。 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>我的测试条件是通过名为quotes.html 的模板并返回一个字符串。(代码中显示的列表)(在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:在引号.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?有没有办法在 Python Flask 中同时返回 html 模板和字符串?

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**

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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