我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用bottle.view()。
def server_static(filename): """ route to the css and static files""" if ".." in filename: return HTTPError(status=403) return bottle.static_file(filename, root='%s/static' % get_config('api', 'view-path', '/etc/softfire/views')) ######### # Utils # #########
def test_view_decorator(self): @view('start {{var}} end') def test(): return dict(var='middle') self.assertEqual(touni('start middle end'), test())
def test_view_decorator_issue_407(self): @view('stpl_no_vars') def test(): pass self.assertEqual(touni('hihi'), test()) @view('aaa {{x}}', x='bbb') def test2(): pass self.assertEqual(touni('aaa bbb'), test2())
def test_view(self): """ WSGI: Test view-decorator (should override autojson) """ @bottle.route('/tpl') @bottle.view('stpl_t2main') def test(): return dict(content='1234') result = '+base+\n+main+\n!1234!\n+include+\n-main-\n+include+\n-base-\n' self.assertHeader('Content-Type', 'text/html; charset=UTF-8', '/tpl') self.assertBody(result, '/tpl')
def test_view_error(self): """ WSGI: Test if view-decorator reacts on non-dict return values correctly.""" @bottle.route('/tpl') @bottle.view('stpl_t2main') def test(): return bottle.HTTPError(401, 'The cake is a lie!') self.assertInBody('The cake is a lie!', '/tpl') self.assertInBody('401 Unauthorized', '/tpl') self.assertStatus(401, '/tpl')
def post_new_comment(): name = bottle.request.forms.get("commentName") email = bottle.request.forms.get("commentEmail") body = bottle.request.forms.get("commentBody") permalink = bottle.request.forms.get("permalink") post = posts.get_post_by_permalink(permalink) cookie = bottle.request.get_cookie("session") username = sessions.get_username(cookie) # if post not found, redirect to post not found error if post is None: bottle.redirect("/post_not_found") return # if values not good, redirect to view with errors if name == "" or body == "": # user did not fill in enough information # init comment for web form comment = {'name': name, 'email': email, 'body': body} errors = "Post must contain your name and an actual comment." return bottle.template("entry_template", dict(post=post, username=username, errors=errors, comment=comment)) else: # it all looks good, insert the comment into the blog post and redirect back to the post viewer posts.add_comment(permalink, name, email, body) bottle.redirect("/post/" + permalink)
def post_new_comment(): name = bottle.request.forms.get("commentName") email = bottle.request.forms.get("commentEmail") body = bottle.request.forms.get("commentBody") permalink = bottle.request.forms.get("permalink") post = posts.get_post_by_permalink(permalink) cookie = bottle.request.get_cookie("session") username = sessions.get_username(cookie) # if post not found, redirect to post not found error if post is None: bottle.redirect("/post_not_found") return # if values not good, redirect to view with errors if name == "" or body == "": # user did not fill in enough information # init comment for web form comment = {'name': name, 'email': email, 'body': body} errors = "Post must contain your name and an actual comment." return bottle.template("entry_template", {"post": post, "username": username, "errors": errors, "comment": comment}) else: # it all looks good, insert the comment into the blog post and redirect # back to the post viewer posts.add_comment(permalink, name, email, body) bottle.redirect("/post/" + permalink) # used to process a like on a blog post