felix 4 years ago
parent
commit
f87ea3577e
3 changed files with 32 additions and 7 deletions
  1. 18 7
      frog.py
  2. 7 0
      templates/404.html
  3. 7 0
      templates/500.html

+ 18 - 7
frog.py

@@ -1,11 +1,14 @@
-from flask import Flask
-from flask import request
-from flask import make_response
-from flask import redirect
-from flask import render_template
-from flask import url_for
+from flask import Flask,request,make_response,redirect,render_template,url_for,abort
 app=Flask(__name__)
 
+@app.errorhandler(404)
+def page_not_found(e):
+    return render_template('404.html'),404
+
+@app.errorhandler(500)
+def internal_server_error(e):
+    return render_template('500.html'),500
+
 @app.route('/')
 def index():
     return render_template('index.html')
@@ -21,4 +24,12 @@ def post(id):
 
 @app.route('/article/')
 def article():
-    return redirect(url_for('posts'))
+    return redirect(url_for('posts'))
+
+from flask import abort
+
+@app.route('/i-love/<obj>')
+def ilove(obj):
+    if obj=='shit':
+        abort(404)
+    return '<h1>I love %s</h1>' % obj

+ 7 - 0
templates/404.html

@@ -0,0 +1,7 @@
+{% extends 'base.html' %}
+
+{% block title %}404 not found{% endblock %}
+
+{% block content %}
+<h1>404 page not found</h1>
+{% endblock %}

+ 7 - 0
templates/500.html

@@ -0,0 +1,7 @@
+{% extends 'base.html' %}
+
+{% block title %}500 internal server error{% endblock %}
+
+{% block content %}
+<h1>500 internal server error</h1>
+{% endblock %}