【问题标题】:Template not rendering properly through flask模板无法通过烧瓶正确渲染
【发布时间】:2016-11-21 10:21:32
【问题描述】:

当我运行我的代码时,它正在我的模板中读取,但是来自所有 html 的标签都显示在我的 div 中,看起来很糟糕,我接下来会寻找添加和图像到它,它不会工作它只会显示我为它编写的代码。

这是我的页面 webapp.py

from flask import Flask, render_template, request

app = Flask(__name__)  

@app.route("/")
def root():
    return app.send_static_file('index.html')

@app.route("/london") 
def name():         
    return "London is the capital and most populous city of England and the United Kingdom."

@app.route("/paris") 
def paris():
    return "Paris is the captial of France and the most populated in France "

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

if __name__ == "__main__":     
    app.run()

这是我的模板文件夹中的 japan.html,我用 japan 按钮调用它。

<!doctype html>
<title>Japan</title>
<h1>Japan </h1> 
<p>Japan is an island nation in the Pacific Ocean with dense cities, imperial palaces, mountainous national parks and thousands of shrines and  temples. </p> 
<p>Shinkansen bullet trains connect the main islands of Kyushu (with Okinawa's subtropical beaches), Honshu (home to Tokyo and Hiroshima’s atomic-bomb memorial) and Hokkaido (famous for skiing).</p>
<p> Tokyo, the capital, is known for skyscrapers, shopping and pop culture.</p>

最后是我的静态文件夹中的 index.html:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#londonbutton").click(function(){
    $.get("/london", function(data, status){
    $("#city").text(data);
    });
});
});
$(document).ready(function(){
    $("#parisbutton").click(function(){
    $.get("/paris", function(data, status){
    $("#city").text(data);
    });
    });
});
$(document).ready(function(){
    $("#japanbutton").click(function(){
    $.get("/japan", function(data, status){
    $("#city").text(data);
});
});
});

</script>
</head>
<body>

<input type="submit" name="London" id="londonbutton" value="Update London">
<input type="submit" name="Paris" id="parisbutton" value="Update Paris">
<input type="submit" name="Japan" id="japanbutton" value="Update Japan">
<h2> Capital City</h2> 
<div id = "city">
<h2>city name</h2>
<p>Some text about a city</p>
</div>



</body> 
</html>

【问题讨论】:

    标签: javascript python html flask get


    【解决方案1】:

    问题出在此处(index.html 中的第 23 行):

    $("#city").text(data);
    

    jQuery 的 .text 将转义所有 HTML(使其仅显示为文本),您需要在此处使用 .html

    $("#city").html(data);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-25
      • 1970-01-01
      • 2021-04-02
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多