Flask, Python, templates: Is it possible to pass variables from ‘index’ html to ‘post’, without using the variable in ‘post’ URL?

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…

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật