Parcourir la source

405 error handle and POST route / handling

michal.korcak il y a 1 mois
Parent
commit
f302b8e436
3 fichiers modifiés avec 21 ajouts et 3 suppressions
  1. 10 3
      main.py
  2. BIN
      static/images/405_meme.jfif
  3. 11 0
      templates/405.html

+ 10 - 3
main.py

@@ -1,7 +1,14 @@
-from flask import Flask, render_template
+from flask import request, Flask, render_template
 
 app = Flask(__name__)
 
-@app.route('/')
+@app.route('/', methods=['GET', 'POST'])
 def hello_world():
-    return render_template('index.html')
+    if request.method == 'GET':
+        return render_template('index.html')
+    elif request.method == 'POST':
+        return request.form
+
+@app.errorhandler(405)
+def method_not_allowed(e):
+    return render_template('405.html'), 405

BIN
static/images/405_meme.jfif


+ 11 - 0
templates/405.html

@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>405 Method Not Allowed</title>
+</head>
+<body>
+    <img src="{{ url_for('static', filename='images/405_meme.jfif') }}" alt="405 Error" style="width: 400px; display: block; margin: 0 auto;">
+</body>
+</html>