예제 #1
0
"""
Templating
"""


@app.route("/template")
def template_handler(request, response):
    response.body = app.get_template("index.html",
                                     context={
                                         "name": "Ackee",
                                         "title": "Legit framework"
                                     }).encode()


@app.route("/exception")
def throw_handler_exception(request, response):
    raise AssertionError("This handler should not be used")


"""
Exceptions
"""


def custom_exception_handler(request, response, exception_cls):
    response.text = str(exception_cls)


app.add_exception_handler(custom_exception_handler)
예제 #2
0
    response.text = "<style>body{background-color:lightblue;}</style><h1>HTML</h1>"


@app.route("/hello/{name}")
def greeting(request, response, name):
    response.text = f"Hello, {name}"


@app.route("/book")
class BookResourse:
    def get(self, req, resp):
        resp.text = "Books Page"


@app.route("/template")
def template_handler(req, resp):
    resp.text = app.template("index.html",
                             context={
                                 "name": "Alien",
                                 "title": "Best Framework"
                             })


@app.route("/exc")
def exception_throwing_handler(request, response):
    raise AssertionError("This handler shouldn't be used")


app.add_exception_handler(exception_handler)
app.add_middleware(SimpleCustomMiddleware)