Context
-
Home page contains post title and subtitle. Also it contains a link to the individual ‘Post’ page.
-
Post page contains title, subtitle, and body.
-
Building both home page and individual post pages with Requests response, and templating with Flask.
-
I’m trying to put the “requests.get” in method “def home()”, so that every time home page is loaded, it get refreshed with new data.
python:
from flask import Flask, render_template
import requests
app = Flask(__name__)
@app.route('/')
def home():
r = requests.get("https://api.npoint.io/c790b4d5cab58020d391")
data = r.json()
print(data)
return render_template("index.html", response_data=data)
@app.route("/URL/post/<num>")
def content(num):
return render_template("post.html", title=title, subtitle=subtitle, body=body, num=num)
if __name__ == "__main__":
app.run(debug=True)
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="https://fonts.googleapis.com/css2?family=Raleway" rel="stylesheet">
<link rel="stylesheet" href="../static/css/styles.css">
</head>
<body>
<div class="wrapper">
<div class="top">
<div class="title"><h1>My Blog</h1></div>
</div>
{% for i in [0, response_data|length-1]: %}
<div class="content">
<div class="card">
<h2>{{ response_data[i].title }}</h2>
<p class="text">{{ response_data[i].subtitle }}</p>
<a href = "{{ url_for('content', num=i, title=response_data[i].title, subtitle=response_data[i].subtitle, body=response_data[i].body) }}">Read</a>
</div>
</div>
{% endfor %}
</div>
</body>
<footer>
<p>Made with ♥️ in London.</p>
</footer>
</html>
post.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="https://fonts.googleapis.com/css2?family=Raleway" rel="stylesheet">
<link rel="stylesheet" href="../static/css/styles.css">
</head>
<body>
<div class="wrapper">
<div class="top">
<div class="title"><h1>{{ title }}</h1></div>
</div>
<div class="content">
<div class="card">
<h2>{{ subtitle }}</h2>
<p>{{ body }}</p>
</div>
</div>
</div>
</body>
<footer>
<p>Made with ♥️ in London.</p>
</footer>
</html>
-
I’m trying to put the “requests.get” in method “def home()”, so that every time home page is loaded, it get refreshed with new data.
-
However, in this way, the response data were held inside of the home() function. I need to access the corresponding title, subtitle, body, num in individual post pages.
-
I can pass <num> because it’s needed in post URL. However, I don’t know how to pass title, subtitle, body to subpages.
My 1st question is, if I want to also pass title, subtitle, and body to the individual post page, so that it can be rendered, how can I do that without adding those information to the post URL?
My 2nd question is, is it the correct thinking to put requests.get in “def home()”? Comparing putting it outside of the home page method, I thought putting it inside can help to refresh home page content everytime it get loaded, instead of the flask server get reloaded. But correct me if I’m wrong.
I’m wondering because if I put it outside of the home() function, I don’t can use response_data[num].title to access the data in post pages, but intuitively, users would want to access the most recent data when home page loads, and it should refresh when home page reloads…