模板无法通过烧瓶正确渲染

时间:2016-11-21 10:21:32

标签: javascript python html flask get

当我运行我的代码时,它正在我的模板中读取,但是所有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,我用日本按钮调用它。

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

1 个答案:

答案 0 :(得分:2)

问题出在这里(index.html第23行):

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

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

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