frog.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from flask import Flask,request,make_response,redirect,render_template,url_for,abort,flash
  2. app=Flask(__name__)
  3. app.config['SECRET_KEY']='132456'
  4. @app.errorhandler(404)
  5. def page_not_found(e):
  6. return render_template('404.html'),404
  7. @app.errorhandler(500)
  8. def internal_server_error(e):
  9. return render_template('500.html'),500
  10. @app.route('/')
  11. def index():
  12. return render_template('index.html')
  13. @app.route('/post/')
  14. def posts():
  15. allposts=[{'id':1,'title':'title1'},{'id':2,'title':'title2'}]
  16. return render_template('posts.html',posts=allposts)
  17. @app.route('/post/<int:id>')
  18. def post(id):
  19. return render_template('post.html',id=id)
  20. @app.route('/article/')
  21. def article():
  22. return redirect(url_for('posts'))
  23. from flask import abort
  24. @app.route('/i-love/<obj>')
  25. def ilove(obj):
  26. if obj=='shit':
  27. abort(404)
  28. return '<h1>I love %s</h1>' % obj
  29. @app.route('/<int:num1>/plus/<int:num2>/eq/<int:num3>')
  30. def count(num1,num2,num3):
  31. if num1+num2==num3:
  32. flash('计算正确')
  33. else:
  34. flash('计算错误')
  35. return redirect(url_for('index'))