我正在尋找創(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>
感謝您的幫助!
默認(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