我通过GitHub页面发布了我在Flask和Python3中创建的应用程序,但似乎当我想在我的页面上点击back
时,它返回https://jurestabuc.github.io/
而不是应用程序的根目录https://jurestabuc.github.io/news-terror-app/build/index.html
。
有人可以告诉我我做错了什么吗?
这是详细信息页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
<title>Terror</title>
</head>
<body>
<header>
<h1>In {{ object.iyear }}, {{ object.nkill }} people died in attacks in {{ object.city }}, {{ object.country_txt }}. The attack was carried out by {{ object.gname }}.</h1>
</header>
<div id="map" style="width:100%; height:300px;"></div>
<p><a href="/">« Back</a></p>
<script type="text/javascript">
var map = L.map("map").setView([{{ object.latitude }}, {{ object.longitude }}], 16);
var osmLayer = new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Data, imagery and map information provided by <a href="http://www.openstreetmap.org/" target="_blank">OpenStreetMap</a>.'
});
map.addLayer(osmLayer);
var marker = L.marker([{{ object.latitude }}, {{ object.longitude }}]).addTo(map);
</script>
</body>
</html>
索引页:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
<title>Terror</title>
</head>
<body>
<!-- <nav>
<a href="">
<img src="">
</a>
</nav> -->
<header>
<h1>50 worst acts of terrorism in Western Europe</h1>
<div class="byline">
By <a href="https://twitter.com/JureStabuc">Jure Stabuc</a>
</div>
</header>
<div id="map" style="width:100%; height:400px;"></div>
<table border=1 cellpadding=7>
<tr>
<th>City</th>
<th>Country</th>
<th>Year</th>
<th>Type of attack</th>
<th>Target</th>
<th>Number of dead</th>
<th>Terrorist organisation</th>
<!-- <th>Motive</th> -->
<!-- <th>Weapons</th> -->
</tr>
{% for obj in object_list %}
<tr>
<td><a href="{{ obj.id }}/">{{ obj.city }}</a></td>
<td><a href="{{ obj.id }}/">{{ obj.country_txt}}</a></td>
<td><a href="{{ obj.id }}/">{{ obj.iyear }}</a></td>
<td><a href="{{ obj.id }}/">{{ obj.attacktype1_txt}}</a></td>
<td><a href="{{ obj.id }}/">{{ obj.target1 }}</a></td>
<td><a href="{{ obj.id }}/">{{ obj.nkill }}</a></td>
<td><a href="{{ obj.id }}/">{{ obj.gname }}</a></td>
<!-- <td><a href="{{ obj.id }}/">{{ obj.motive }}</a></td> -->
<!-- <td><a href="{{ obj.id }}/">{{ obj.weapdetail}}</a></td> -->
</tr>
{% endfor %}
</table>
<script type="text/javascript">
var map = L.map("map").setView([50.0000, 20.0000], 4);
var osmLayer = new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: 'Data, imagery and map information provided by <a href="http://www.openstreetmap.org/" target="_blank">OpenStreetMap</a>.'
});
map.addLayer(osmLayer);
var data = {
"type": "FeatureCollection",
"features": [
{% for obj in object_list %}
{
"type": "Feature",
"properties": {
"city": "{{ obj.city }}",
"id": "{{ obj.id }}"
},
"geometry": {
"type": "Point",
"coordinates": [{{ obj.longitude }}, {{ obj.latitude }}]
}
}{% if not loop.last %},{% endif %}
{% endfor %}
]
};
var dataLayer = L.geoJson(data, {
onEachFeature: function(feature, layer) {
layer.bindPopup(
"<a href = '" + feature.properties.id + "/'>" + feature.properties.city + "</a>"
);
}
});
map.addLayer(dataLayer);
</script>
</body>
</html>
app.py:
import csv
from flask import Flask
from flask import abort
from flask import render_template
app = Flask(__name__)
def get_csv():
csv_path ="./static/top50weu.csv"
#adding encoding because of issues with it. http://stackoverflow.com/questions/12468179/unicodedecodeerror-utf8-codec-cant-decode-byte-0x9c
csv_file = open(csv_path, "r", encoding="latin-1")
#parsed and returned as a list of dictionaries
csv_obj = csv.DictReader(csv_file)
csv_list = list(csv_obj)
return csv_list
@app.route("/")
def index():
template ="index.html"
object_list = get_csv()
return render_template(template, object_list=object_list)
@app.route("/<row_id>/")
def detail(row_id):
template = "detail.html"
object_list = get_csv()
for row in object_list:
if row["id"] == row_id:
return render_template(template, object=row)
abort(404)
if __name__ == "__main__":
#Fire up the Flask test server
app.run(debug=True, use_reloader=True)
freeze.py:
from flask_frozen import Freezer
from app import app, get_csv
freezer = Freezer(app)
app.config['FREEZER_RELATIVE_URLS'] = True
@freezer.register_generator
def detail():
for row in get_csv():
yield {"row_id": row["id"]}
if __name__ == '__main__':
freezer.freeze()
当地一切都很好。示例位于:app感谢您的帮助!
答案 0 :(得分:2)
我认为它不是<a href="/">« Back</a>
我使用<a href="{{ url_for('index') }}">« Back</a>
而且工作正常。