app.py
python_backend/curriculum/flask/app.py
from flask import Flask, render_template, request, redirect, url_for, flash
app = Flask(__name__)
app.secret_key = "super_secret_key_for_learning"
# In-memory "database" for the guestbook
messages = [
{"name": "Alice", "content": "Welcome to the Flask lesson!"},
{"name": "Bob", "content": "Jinja2 is really powerful."}
]
@app.route("/")
def index():
"""Home page showing all guestbook entries"""
return render_template("index.html", messages=messages)
@app.route("/post", methods=["POST"])
def post_message():
"""Handle form submission to add a new message"""
name = request.form.get("name")
content = request.form.get("content")
if not name or not content:
flash("Both name and content are required!", "error")
else:
messages.append({"name": name, "content": content})
flash("Message added successfully!", "success")
return redirect(url_for("index"))
if __name__ == "__main__":
app.run(debug=True)
Articoli correlati
apps.py
apps.py — python source code from the python backend learning materials (python_backend/curriculum/django/blog/apps.py).
Leggi l'articolo →0001_initial.py
0001_initial.py — python source code from the python backend learning materials (python_backend/curriculum/django/blog/migrations/0001_initial.py).
Leggi l'articolo →models.py
models.py — python source code from the python backend learning materials (python_backend/curriculum/django/blog/models.py).
Leggi l'articolo →urls.py
urls.py — python source code from the python backend learning materials (python_backend/curriculum/django/blog/urls.py).
Leggi l'articolo →views.py
views.py — python source code from the python backend learning materials (python_backend/curriculum/django/blog/views.py).
Leggi l'articolo →manage.py
manage.py — python source code from the python backend learning materials (python_backend/curriculum/django/manage.py).
Leggi l'articolo →