亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

使用Jinja模板/Flask實(shí)現(xiàn)帶有加粗修飾的嵌套HTML段落渲染
P粉170438285
P粉170438285 2024-03-22 13:59:22
0
1
994

我正在尋找創(chuàng)建一個(gè)函數(shù),該函數(shù)可以從句子中刪除單詞,然后用從字典API搜索中獲取的其他單詞替換被刪除的單詞。

非常簡(jiǎn)單,這是一個(gè)檢查句子中的單詞是否屬于被刪除單詞列表的函數(shù),如果是,則用替代單詞替換,如果不是,則將原始單詞添加到新字符串中。沒(méi)有問(wèn)題,

我需要幫助的是,如果我使用F字符串并添加文本修飾符以在HTML標(biāo)記中解釋,這樣做的方式是正確的嗎?我只想加粗替換的文本

if word in removed_words:
               print("our word for the dictionary is", word)  
               res =  dictionary.meaning(word.capitalize())

               if res != None:
              
                   if res.get('Noun'):
                      print("our definition is", "---> ", res['Noun'][0], " <----")
                      remaining_words.append(f"""{res['Noun'][0]}""")

                   elif res.get('Verb'):
                        print("our definition is", "---> ", res['Verb'][0], " <----")
                        remaining_words.append(f"""{res['Verb'][0]}""")

               else:
                    remaining_words.append(f"""{r.word()}""")
             
           else:
                remaining_words.append(word)

當(dāng)我在瀏覽器中檢查HTML標(biāo)記時(shí),包含新字符串的段落元素被正確編譯,例如

<p>This is the new sentence with the <b>replaced word</b> and the other words</p>

然而問(wèn)題是,最終的標(biāo)記中隱含了<b>,但沒(méi)有被渲染出來(lái)。 我在這里漏掉了什么嗎?

在渲染過(guò)程中,段落被調(diào)用到的Flask模板標(biāo)記如下,包含問(wèn)題[0]的<p>是我討論的新渲染字符串值。

h3 class="header3 head4">{{heading}}

<p id="question">{{question[0]}}</p>

<button id="showanswer">Show the Answer</button>
<p id="answer">{{question[1]}}</p>

<form  id="submitanswer" method="post", action="/quiz/processanswer">
<input id="useranswer" type="text" name="answer" placeholder="Enter your answer">
<input id="hiddenanswer" name="hiddenanswer" type="text" value="{{question[1]}}" 
 id="hiddenanswer">
 <button id="answerSubmit">Submit</button>
 </form>

感謝您的幫助!

P粉170438285
P粉170438285

全部回復(fù)(1)
P粉087074897

默認(rèn)情況下,Jinga會(huì)自動(dòng)轉(zhuǎn)義變量中的字符,如 >、<(當(dāng)使用{{question[0]}}時(shí))。

如果你對(duì)question[0]的構(gòu)造方式有信心,你可以通過(guò)將<p id="question">{{question[0]}}</p>改為<p id="question">{{question[0] | safe }}</p>來(lái)繞過(guò)這種自動(dòng)轉(zhuǎn)義。

更多信息請(qǐng)參考:https://jinja.palletsprojects.com/en/3.0.x/templates/#html-escaping

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板