flask中在网页上显示js代码

2020-03-26 10:11:00 by wst

flask

在网页中显示js代码,这个问题困扰了我好久,今天终于找到方法。

背景:

这里是个存储配置的表,当用户填完配置信息,要展示js中是怎么调用的。

原始字符串为:

# domain_pre,bi_sdk,version,app_key 为要填充的信息
http_script = """
<script src="http://{domain_pre}.xxx.cn/{bi_sdk}.{version}.js"></script>
<script> 
// window.{bi_sdk_upper}.debug = true;
window.BI_SDK.report('{app_key}','{domain_pre}');
</script>  
"""

曾经做了以下尝试:

1. 使用safe过滤器

<td style="word-break:break-all; ">
{{ inner|safe }}
</td>

结果失败,网页会把它当成真正的script代码

2. 使用json化方法

<td style="word-break:break-all; ">
{{ inner|tojson }}
</td>

结果是能显示了,但是<>被编码了。看起来很是费解。

3. 自定义过滤器

from flask import render_template_string

def script_convert(scri):
    temp = render_template_string("{{code}}", code=scri)
    return temp

网页中代码:

<td style="word-break:break-all; ">
{{ inner|script_convert }}
</td>

 结果依然对<>进行了编码,只是编码方式不一样了而已,看起同样费解。

最终解决方案

结合使用safe和自定义过滤器

<td style="word-break:break-all; ">
{{ inner|script_convert|safe }}
</td>

 

防止小白看完依然不知道怎么使用,这里给出完整的使用流程:

综合使用步骤:

1. 定义过滤器,假如文件为run.py,下面的Python代码都在此文件中。

from flask import render_template_string

def script_convert(scri):
    temp = render_template_string("{{code}}", code=scri)
    return temp

 2. 在app中注册过滤器

from flask import Flask

app = Flask(__name__, instance_relative_config=True)

app.add_template_filter(script_convert, "script_convert")

3. 在模板中使用此过滤器,假设模板文件为index.html,这里的inner为上文的http_script变量

<td style="word-break:break-all; ">
{{ inner|script_convert|safe }}
</td>

 

如还有其他问题,请在下方留言! 


Comments(130) Add Your Comment

Charlesnag
“I haven’t seen you in these parts,” the barkeep said, sidling over and above to where I sat. “Repute’s Bao.” He stated it exuberantly, as if say of his exploits were shared by settlers hither many a verve in Aeternum. He waved to a unimpassioned hogshead beside us, and I returned his token with a nod. He filled a glass and slid it to me across the stained red wood of the bar in the vanguard continuing. “As a betting fellow, I’d be ready to wager a adequate bit of silver you’re in Ebonscale Reach for the purpose more than the drink and sights,” he said, eyes glancing from the sword sheathed on my cool to the bend slung across my back. https://images.google.it/url?q=https://renewworld.ru/data-vyhoda-new-world/
Ziebjz
order lipitor 40mg for sale <a href="https://lipiws.top/">order atorvastatin 80mg sale</a> atorvastatin online
Aovlzo
ciprofloxacin 500mg generic - <a href="https://metroagyl.top/keflexcapsule/">order keflex 500mg generic</a> buy amoxiclav for sale
Qppjrb
buy ciprofloxacin 500mg online cheap - <a href="https://cipropro.top/gemyambutol/">order myambutol 1000mg online cheap</a> amoxiclav oral
Gpucoi
metronidazole online buy - <a href="https://metroagyl.top/keflexcapsule/">amoxil oral</a> azithromycin brand
Vekljw
ciplox pills - <a href="https://septrim.top/doryx/">doxycycline usa</a> brand erythromycin 500mg
Izqaym
valtrex 500mg canada - <a href="https://gnantiviralp.com/zovirax/">zovirax 800mg pill</a> acyclovir cost
Lttfmb
ivermectin 3mg for humans - <a href="https://keflexin.top/aczone/">where can i buy aczone</a> generic tetracycline 500mg
Shldgz
flagyl 200mg price - <a href="https://metroagyl.top/azithromycin/">buy zithromax 500mg pills</a> buy cheap zithromax
Ovxzku
order generic ampicillin <a href="https://ampiacil.top/doxycyclineantb/">buy generic doxycycline over the counter</a> amoxicillin buy online
Zuucnq
purchase lasix online - <a href="https://antipathogenc.com/medex/">buy warfarin medication</a> oral capoten
Pxupsm
glycomet cost - <a href="https://gnantiacidity.com/combivir/">buy combivir generic</a> lincocin 500 mg cheap
Okxdmu
retrovir 300mg for sale - <a href="https://canadiangnp.com/avapro/">buy irbesartan no prescription</a> where can i buy allopurinol
Aihlrk
clozaril oral - <a href="https://genonlinep.com/">clozapine 100mg for sale</a> pepcid tablet
Cejmsh
buy quetiapine 100mg online cheap - <a href="https://gnkantdepres.com/eskalith/">order eskalith sale</a> eskalith over the counter
Fyrzgt
cheap clomipramine - <a href="https://antdeponline.com/asendin/">asendin 50mg over the counter</a> order doxepin 75mg without prescription
Txvomh
atarax 25mg brand - <a href="https://antdepls.com/lexapro/">oral escitalopram 20mg</a> endep 10mg brand
Duzisf
buy amoxicillin pills - <a href="https://atbioxotc.com/ceftin/">cefuroxime 500mg cost</a> purchase ciprofloxacin sale
Fayoqa
buy augmentin 1000mg for sale - <a href="https://atbioinfo.com/septra/">septra drug</a> purchase cipro pills
Xvazsr
order cleocin pill - <a href="https://cadbiot.com/cefixime/">cefixime oral</a> chloromycetin pills
Vksecu
buy generic azithromycin for sale - <a href="https://gncatbp.com/ctinidazole/">generic tinidazole 300mg</a> ciplox 500 mg usa
Wsfzwm
where to buy ivermectin - <a href="https://antibpl.com/ascefaclor/">cefaclor 250mg without prescription</a> cefaclor over the counter
Qtgkak
best allergy pills - <a href="https://antxallergic.com/xsseroflo/">order seroflo for sale</a> theophylline pill
Dtyahm
desloratadine price - <a href="https://rxtallerg.com/getaristocort/">brand triamcinolone</a> albuterol 4mg cheap
Krlpvi
cost of depo-medrol - <a href="https://ntallegpl.com/montelukast/">montelukast 10mg oral</a> order astelin 10 ml sale
Pvcyxq
glyburide 5mg pills - <a href="https://prodeprpl.com/pioglitazone30/">buy pioglitazone 30mg</a> forxiga 10mg over the counter
Tlmyep
repaglinide 1mg brand - <a href="https://depressinfo.com/jardiance25/">jardiance 25mg cost</a> order empagliflozin 10mg without prescription
Zjocha
glycomet 500mg usa - <a href="https://arxdepress.com/acarbose50/">acarbose 50mg drug</a> precose buy online
Soaasm
order terbinafine 250mg - <a href="https://treatfungusx.com/xgrifulvinvs/">buy grifulvin v online cheap</a> grifulvin v canada
Mnvzro
rybelsus 14mg canada - <a href="https://infodeppl.com/glucovance5/">where to buy glucovance without a prescription</a> DDAVP online buy
Vrugsw
purchase ketoconazole pill - <a href="https://antifungusrp.com/itraconazole100/">buy itraconazole medication</a> buy itraconazole 100mg pill
Zsrbnl
famciclovir online buy - <a href="https://amvinherpes.com/gnvalaciclovir/">order valcivir 500mg generic</a> valaciclovir 1000mg sale
Qazpbz
digoxin 250mg pills - <a href="https://blpressureok.com/furosemidediuret/">lasix 40mg ca</a> lasix 40mg for sale
Jsvowj
microzide 25 mg cheap - <a href="https://norvapril.com/">order microzide 25mg online</a> buy zebeta no prescription
Gdpsgv
order generic lopressor 50mg - <a href="https://bloodpresspl.com/nifedipine/">brand adalat</a> generic adalat 30mg
Ahcbxz
nitroglycerin for sale - <a href="https://nitroproxl.com/valsartan/">order diovan pills</a> valsartan usa
Jvcudz
simvastatin wrought - <a href="https://canescholest.com/gemfibrozil/">gemfibrozil gaz</a> lipitor sheet
Wlkvib
crestor per - <a href="https://antcholesterol.com/caduet5mg/">caduet buy root</a> caduet online much
Brhdcr
viagra professional stride - <a href="https://edsildps.com/superkamagra/">super kamagra goblin</a> levitra oral jelly doubt
Wfaonz
dapoxetine limb - <a href="https://promedprili.com/suhagra/">suhagra fire</a> cialis with dapoxetine mum
Uskojn
cenforce online wipe - <a href="https://xcenforcem.com/tadalissxtadalafil/">tadalis pills lend</a> brand viagra pills announce
Ukfazo
brand cialis fred - <a href="https://probrandtad.com/zhewitra/">zhewitra consider</a> penisole pain
Jxcqua
cialis soft tabs betray - <a href="https://supervalip.com/valifonline/">valif pills darken</a> viagra oral jelly online shave
Kvohlo
brand cialis government - <a href="https://probrandtad.com/">brand cialis rough</a> penisole jar
Skpyfw
cialis soft tabs online single - <a href="https://supervalip.com/">cialis soft tabs online carve</a> viagra oral jelly design
Kdlpez
cenforce shawn - <a href="https://xcenforcem.com/kamagrasildenafil/">kamagra pills lock</a> brand viagra online whether
Jeeito
dapoxetine eagle - <a href="https://promedprili.com/viagraplus/">viagra plus add</a> cialis with dapoxetine england
Jgzefc
acne medication silver - <a href="https://placnemedx.com/">acne medication describe</a> acne treatment amount
Lxiwjt
asthma medication ordinary - <a href="https://bsasthmaps.com/">asthma medication sum</a> asthma medication progress
Kvbjbs
treatment for uti virtue - <a href="https://amenahealthp.com/">uti treatment snort</a> uti treatment section
Hxgdet
prostatitis pills request - <a href="https://xprosttreat.com/">prostatitis treatment write</a> prostatitis treatment sorrow
Znshfw
valtrex pills hair - <a href="https://gnantiviralp.com/">valacyclovir pills cast</a> valtrex online pair
Pkhcun
claritin pills pop - <a href="https://clatadine.top/">claritin pills castle</a> loratadine parent
Swpuib
loratadine spy - <a href="https://clatadine.top/">loratadine attend</a> claritin bosom
Ilcdyq
dapoxetine fred - <a href="https://prilixgn.top/">priligy color</a> dapoxetine monk
Qgzgbx
promethazine degree - <a href="https://prohnrg.com/">promethazine russian</a> promethazine bargain
Dxpjrt
ascorbic acid clatter - <a href="https://ascxacid.com/">ascorbic acid parent</a> ascorbic acid release
Gffylk
biaxin equipment - <a href="https://gastropls.com/gncytotec/">cytotec petunia</a> cytotec themself
Aaqzpd
florinef two - <a href="https://gastroplusp.com/prlansoprazole/">prevacid pills day</a> lansoprazole attach
Hztzfe
buy generic dulcolax for sale - <a href="https://gastroinfop.com/oxybutynin/">oxytrol online order</a> liv52 20mg cheap
Vwpiws
order rabeprazole 20mg sale - <a href="https://gastrointesl.com/domperidone/">buy motilium 10mg</a> buy generic domperidone for sale
Btnkou
cotrimoxazole 480mg brand - <a href="https://tobmycin.com/">tobramycin 5mg drug</a> tobra 10mg uk
Flvlfu
purchase zovirax online cheap - <a href="https://danaterone.shop/">brand hydroquinone</a> duphaston 10 mg for sale
Xerzpg
purchase dapagliflozin for sale - <a href="https://precarbos.com/">buy acarbose 25mg pills</a> precose uk
Tcebrg
buy fulvicin 250 mg for sale - <a href="https://fulviseoful.com/">griseofulvin 250 mg cost</a> buy gemfibrozil without a prescription
Yysfzs
enalapril 5mg us - <a href="https://vasolapril.com/">vasotec 10mg drug</a> xalatan brand
Apryqz
order dramamine 50 mg online - <a href="https://bisacolax.com/">dramamine 50 mg without prescription</a> order risedronate 35mg generic
Zatwnt
buy piroxicam pills for sale - <a href="https://rivastilons.com/">buy exelon 3mg pills</a> buy generic rivastigmine 6mg
Npxwqx
buy nootropil generic - <a href="https://nootquin.com/efavirenz/">buy generic sustiva</a> buy sinemet medication
Jayxba
cost hydrea - <a href="https://hydroydrinfo.com/disulfiram/">disulfiram 250mg usa</a> order methocarbamol 500mg online cheap
Eckjzb
depakote 250mg sale - <a href="https://adepamox.com/acetazolamide/">buy acetazolamide generic</a> topamax pill
Kigadp
buy generic disopyramide phosphate for sale - <a href="https://anorpica.com/chlorpromazine/">cost thorazine 50 mg</a> buy thorazine 50 mg without prescription
Dqyokl
purchase cyclophosphamide without prescription - <a href="https://cycloxalp.com/trimetazidine/">trimetazidine ca</a> buy generic vastarel over the counter
Chvzbm
aldactone for sale - <a href="https://aldantinep.com/naltrexone/">order revia 50mg</a> naltrexone 50 mg generic
Tiaydp
buy generic cyclobenzaprine for sale - <a href="https://abflequine.com/enalapril/">buy enalapril 10mg online cheap</a> buy vasotec 10mg
Qypsvd
order ondansetron 8mg for sale - <a href="https://azofarininfo.com/">buy zofran paypal</a> ropinirole over the counter
Kitrcn
buy ascorbic acid medication - <a href="https://mdacidinfo.com/isosorbidedinitrate/">cheap isordil pills</a> how to get prochlorperazine without a prescription
Evylxj
buy durex gel sale - <a href="https://xalaplinfo.com/latanoprosteyedrops/">buy xalatan for sale</a> buy latanoprost paypal
Yrayfn
purchase arava - <a href="https://infohealthybones.com/">order arava 10mg online</a> order cartidin without prescription
Mnyqcg
cheap atenolol 100mg - <a href="https://heartmedinfox.com/clopidogrel/">oral plavix 150mg</a> carvedilol online
Ykbgdg
order verapamil sale - <a href="https://infoheartdisea.com/atenolol/">order tenoretic online cheap</a> purchase tenoretic generic
Xjxwsp
purchase gasex pills - <a href="https://herbalinfomez.com/">gasex generic</a> diabecon for sale online
Srjibj
oral lasuna - <a href="https://infoherbalmz.com/himcolin/">himcolin tablets</a> himcolin tablet
Omtnkr
buy noroxin pills - <a href="https://gmenshth.com/flutamide/">eulexin online buy</a> oral confido
Vphijz
speman generic - <a href="https://spmensht.com/himplasia/">order generic himplasia</a> cheap generic fincar
Jzmczm
purchase finasteride for sale - <a href="https://finmenura.com/doxazosin/">how to get doxazosin without a prescription</a> alfuzosin 10 mg uk
Rtilmc
generic terazosin 1mg - <a href="https://hymenmax.com/tamsulosin/">tamsulosin 0.2mg usa</a> priligy 90mg generic
Pcxajp
purchase oxcarbazepine - <a href="https://trileoxine.com/">order oxcarbazepine 300mg pills</a> buy synthroid generic
Wvvxtr
where to buy lactulose without a prescription - <a href="https://duphalinfo.com/">duphalac for sale</a> where can i buy betahistine
Delovm
buy cyclosporine online cheap - <a href="https://asimusxate.com/gloperba/">generic colcrys 0.5mg</a> colchicine 0.5mg us
Whiasc
order calcort for sale - <a href="https://lazacort.com/brimonidine/">order alphagan online</a> buy generic brimonidine for sale
Alipym
buy generic besifloxacin online - <a href="https://besifxcist.com/sildamax/">cheap sildamax pill</a> buy generic sildamax
Kqozjq
order gabapentin 600mg without prescription - <a href="https://aneutrin.com/">cost gabapentin</a> sulfasalazine 500 mg canada
Cdnuxl
probenecid sale - <a href="https://bendoltol.com/">brand probenecid 500 mg</a> tegretol 400mg over the counter
Ogjvqb
celebrex 200mg pills - <a href="https://celespas.com/">buy celecoxib generic</a> purchase indocin online cheap
Fyemon
mebeverine sale - <a href="https://coloxia.com/pletal/">cilostazol price</a> buy generic pletal 100 mg
Ygagoo
purchase diclofenac without prescription - <a href="https://dicloltarin.com/">voltaren price</a> order generic aspirin 75mg
Dvdhbt
brand rumalaya - <a href="https://rumaxtol.com/amitriptyline/">buy elavil 50mg online</a> order elavil 10mg without prescription
Injmfq
order generic mestinon 60mg - <a href="https://mestonsx.com/">pyridostigmine where to buy</a> buy azathioprine 50mg generic
Qbbkog
purchase voveran generic - <a href="https://vovetosa.com/">cheap voveran without prescription</a> buy nimotop pills
Ebsvoh
baclofen 25mg for sale - <a href="https://baclion.com/nspiroxicam/">feldene generic</a> order generic feldene 20 mg
Ykzyzb
order mobic 7.5mg pill - <a href="https://meloxiptan.com/rtrizatriptan/">buy generic toradol for sale</a> buy generic ketorolac
Fppaim
purchase cyproheptadine for sale - <a href="https://periheptadn.com/">cost cyproheptadine 4 mg</a> order tizanidine for sale
Jgmmug
cefdinir 300 mg canada - <a href="https://omnixcin.com/clindamycin/">buy cheap clindamycin</a> cheap cleocin
Efrahp
purchase accutane pills - <a href="https://aisotane.com/asdeltasone/">buy deltasone 40mg pill</a> deltasone 10mg pills
Nwuwuz
order deltasone 20mg for sale - <a href="https://apreplson.com/prednisolone/">omnacortil online order</a> permethrin creams
Cofxnm
acticin generic - <a href="https://actizacs.com/tretinoin/">tretinoin online</a> purchase tretinoin sale
Grepcw
order betamethasone 20 gm online - <a href="https://betnoson.com/">order betamethasone 20 gm cream</a> purchase monobenzone online cheap
Qsurya
flagyl 200mg without prescription - <a href="https://cenforcevs.com/">cenforce order online</a> cenforce brand
Rzumyf
amoxiclav brand - <a href="https://alevonted.com/">synthroid 75mcg cheap</a> synthroid 75mcg pills
Kvlxmx
buy clindamycin generic - <a href="https://clinycinpl.com/">cleocin 300mg pill</a> order indocin 50mg pill
Sdobdy
brand cozaar 50mg - <a href="https://cozartan.com/">purchase hyzaar online</a> buy generic cephalexin for sale
Zvmuqd
purchase crotamiton gel - <a href="https://aeuracream.com/">buy eurax medication</a> aczone ca
Oqpeni
buy cheap modafinil - <a href="https://sleepagil.com/melatonin/">meloset without prescription</a> cheap melatonin 3 mg
Cmzxru
zyban 150 mg over the counter - <a href="https://bupropsl.com/">bupropion pill</a> buy shuddha guggulu for sale
Dbrgud
order prometrium for sale - <a href="https://apromid.com/gynelotrimin/">cost ponstel</a> cheap clomiphene sale
Oxwcno
oral capecitabine 500mg - <a href="https://xelocap.com/mefenamicacid/">purchase mefenamic acid pills</a> buy danazol 100 mg generic
Hhmsml
alendronate 35mg oral - <a href="https://pilaxmax.com/pilex/">pilex over the counter</a> provera us
Piwknh
aygestin 5mg tablet - <a href="https://norethgep.com/bimatoprost/">buy lumigan cheap</a> buy generic yasmin
Tuarqp
order dostinex 0.5mg pills - <a href="https://adostilin.com/alesse/">how to get alesse without a prescription</a> cheap alesse tablets
Ecqzet
estradiol over the counter - <a href="https://festrolp.com/">buy cheap generic yasmin</a> anastrozole 1 mg cheap
Kimyrx
г‚·гѓ«гѓ‡гѓЉгѓ•г‚Јгѓ« - 50mg/100mg - <a href="https://jpedpharm.com/tadalafil/">г‚·г‚ўгѓЄг‚№ е‰ЇдЅњз”Ё</a> г‚їгѓЂгѓ©гѓ•г‚Јгѓ« е‰ЇдЅњз”Ё
Fwjmqe
プレドニン通販で買えますか - <a href="https://jpaonlinep.com/">プレドニン通販 安全</a> アジスロマイシン通販で買えますか
Zqdrpc
プレドニンは薬局で買える? - <a href="https://jpanfarap.com/">プレドニンは薬局で買える?</a> イソトレチノイン 薬局で買える
Aygpoy
eriacta palace - <a href="https://eriagra.com/szenegrag/">zenegra french</a> forzest gap
Ljhazi
how to buy crixivan - <a href="https://confindin.com/confido/">how to buy confido</a> order cheap diclofenac gel
Ltrmba
valif pills teeth - <a href="https://avaltiva.com/awsustiva/">buy sustiva 10mg pill</a> buy sinemet 20mg generic
Scrbha
provigil brand - <a href="https://provicef.com/lamivudine/">epivir online buy</a> lamivudine usa
Kanlqv
ivermectin 12mg without prescription - <a href="https://ivercand.com/">ivermectin 12 mg over counter</a> purchase carbamazepine online cheap
Abbxxn
promethazine price - <a href="https://prometacin.com/jilincomycin/">lincocin ca</a> buy lincocin 500 mg generic