async def example1(request: Request, id: int): # Template should be in http/views/xx_appname/template.j2 # All web routes require request: Request # All template views require the request be piped through # To see all route details run ./uvicore package list return response.View('xx_appname/template.j2', { 'request': request, 'id': id, })
async def home(request: Request, user: User = Guard()): #from uvicore.http.response import Redirect #return Redirect('/wiki') dump(user) user = request.user #return user.email return response.View('app1/admin.j2', { 'request': request, 'user': user, })
async def home(request: Request, referer: Optional[str] = None): user = request.user if not user.authenticated: from uvicore.http.exceptions import NotAuthenticated raise NotAuthenticated( headers={ 'WWW-Authenticate': 'Basic realm="App1 Web Realm"' }) if referer: return response.Redirect(referer) return response.View('app1/login.j2', {'request': request})
async def home(request: Request): from uvicore.http.exceptions import NotFound, PermissionDenied, NotAuthenticated, InvalidCredentials, HTTPException from uvicore.exceptions import SmartException raise SmartException('smart here', 404) #raise HTTPException(404, 'blah blah detail', message='nop f', extra={'opt1': 'there'}) #raise NotFound('There was a problem when trying to find the <b>home</b> page :(') #raise PermissionDenied(['one', 'two']) #raise NotAuthenticated('no way') #raise InvalidCredentials('asdfasdf') # raise NotFound({ # 'stuff': 'one', # 'stuff2': 'two', # }) return response.View('app1/home.j2', {'request': request})
async def web(request: Request, e: HTTPException) -> response.HTML: """Main exception handler for all Web endpoints""" # Defined in the running app config api.exceptions.main headers = getattr(e, "headers", None) dump(e.__dict__) try: # Try to response with a errors template, if exists return response.View('errorsx/' + str(e.status_code) + '.j2', { 'request': request, **e.__dict__, }) except: # Errors template does not exist, response with generic HTML error html = f""" <div class="error"> <h1>{e.status_code} {e.message}</h1> {e.detail or ''} </div> """ return response.HTML(content=html, status_code=e.status_code, headers=headers)
async def home(request: Request): return response.View('wiki/about.j2', {'request': request})
async def home(request: Request): return response.View('app1/home.j2', {'request': request})
async def welcome(request: Request): # Example Jinja2 Template return response.View('appstub/welcome.j2', {'request': request})
async def about(request: Request): return response.View('app1/about.j2', { 'request': request })