Day 59, 60 Discord 分享:POST Requests with Flask and HTML Forms

▌導讀:Andy
2022/10/13 PM 9:00

▌本次主題:
Day 59 - Blog Capstone Project Part 2 - Adding Styling(下)
Day 60 - Make POST Requests with Flask and HTML Forms

▌課程共同學習主頁:

▌筆記:
以程式說明

▌線上討論及補充:
• Albert from Discord
我Day 59 /60 跑老師的final code 都會遇到 下列 type Error 要看andy 跑的時候有沒有遇到
any good Idea?

• Tim from Discord
在 for loop 之前
先做一個判斷
if all_posts is None:

• Andy from Discord

from flask import Flask, render_template, request
import requests

response = requests.get("https://api.npoint.io/4ad159e5d42e6a189ef8")
posts = response.json()

app = Flask(__name__)

@app.route('/')
def get_all_posts():
    return render_template("index.html", all_posts=posts)


@app.route('/about')
def about():
    return render_template("about.html")


@app.route('/contact', methods=["GET", "POST"])
def contact():
    if request.method == "POST":
        data = request.form
        print(data["name"])
        print(data["email"])
        print(data["phone"])
        print(data["message"])
        return render_template("contact.html", msg_sent=True)
    return render_template("contact.html", msg_sent=False)

@app.route('/post/<int:index>')
def show_post(index):
    requested_post = None
    for blog_post in posts:
        if blog_post["id"] == index:
            requested_post = blog_post
    return render_template("post.html", post=requested_post)

# @app.route('/form-entry')
# def receive_data():
#     data = request.form
#     print(data["name"])
#     print(data["email"])
#     print(data["phone"])
#     print(data["message"])
#     return "<h1>Successfully sent your message</h1>"


if __name__ == "__main__":
    app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
    <head>
        {% include "header.html" %}
    </head>
    <body>
        <!-- Page Header-->
        <header class="masthead" style="background-image: url( {{ url_for('static', filename='img/home-bg.jpg') }})">
            <div class="container position-relative px-4 px-lg-5">
                <div class="row gx-4 gx-lg-5 justify-content-center">
                    <div class="col-md-10 col-lg-8 col-xl-7">
                        <div class="site-heading">
                            <h1>Clean Blog</h1>
                            <span class="subheading">A Blog Theme by Start Bootstrap</span>
                        </div>
                    </div>
                </div>
            </div>
        </header>
        <!-- Main Content-->
        <div class="container px-4 px-lg-5">
            <div class="row gx-4 gx-lg-5 justify-content-center">
                <div class="col-md-10 col-lg-8 col-xl-7">
                    {% for post in all_posts %}
                    <!-- Post preview-->
                    <div class="post-preview">
                        <a href="{{ url_for('show_post', index=post.id) }}">
                            <h2 class="post-title">
                                {{post.title}}
                            </h2>
                            <h3 class="post-subtitle">
                                {{post.subtitle}}
                            </h3>
                        </a>
                        <p class="post-meta">
                            Posted by
                            <a href="#!">{{post.author}}</a>
                            on {{post.dates}}
                        </p>
                    </div>
                    {% endfor %}
                    <!-- Divider-->
                    <hr class="my-4" />
                    <!-- Pager-->
                    <div class="d-flex justify-content-end mb-4"><a class="btn btn-primary text-uppercase" href="#!">Older Posts →</a></div>
                </div>
            </div>
        </div>
    </body>
    <footer>
       {% include "footer.html" %}
    </footer>
</html>