Dylan
Dylan

Reputation: 83

Template not rendering properly through flask

When I run my code it is reading in my template but the tags from all the html are showing in my div with looks terrible and I will be looking to add and image to it next and it wont work it'll only show the code I have written for it.

This is my page called 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()

This is my japan.html in my templates folder which I am called with the japan button.

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

And finally my index.html which is in my static folder :

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

Upvotes: 0

Views: 524

Answers (1)

chrki
chrki

Reputation: 6323

The problem is here (line 23 in index.html):

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

jQuery's .text will escape all HTML (so that it appears as just text), you need to use .html here instead.

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

Upvotes: 2

Related Questions