Skip to content

Instantly share code, notes, and snippets.

@nomadekr
Last active January 28, 2020 15:02
Show Gist options
  • Save nomadekr/611ad7a6afb0289a9ac19333bd8f8c33 to your computer and use it in GitHub Desktop.
Save nomadekr/611ad7a6afb0289a9ac19333bd8f8c33 to your computer and use it in GitHub Desktop.
AWS chalice 에서 jinja2 템플릿 활용하기

AWS chalice 에서 jinja2 템플릿 활용하기

디렉토리 구조

  • app.py
  • requirements.txt
  • chalicelib/
    • tempaltes/
      • index.html

chalice에는 render_template이 없어서, 직접 jinja2 environment를 생성토록 해봤습니다.

templates 디렉토리는 같이 팩키징이 안 되길래, chalicelib/templates 경로에 넣었습니다.


여러분의 파이썬/장고 페이스메이커가 되겠습니다.

from chalice import Chalice, Response
from jinja2 import Environment, PackageLoader, select_autoescape
app = Chalice(app_name='askdjango-chalice')
env = Environment(
loader=PackageLoader(__name__, 'chalicelib/templates'),
autoescape=select_autoescape(['html', 'xml']))
def render_template(template_name, context, status_code=200, content_type='text/html', headers=None):
template = env.get_template(template_name)
body = template.render(**context)
if headers is None:
headers = {}
headers.update({
'Content-Type': content_type,
})
return Response(body=body, status_code=status_code, headers=headers)
@app.route('/')
def index():
return render_template('index.html', {
'name': 'AskDjango'
})
<!-- 필히 chalicelib/templates/ 경로에 생성해주세요. -->
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<style>
body {
background-color: lightyellow;
}
</style>
</head>
<body>
Hello {{ name }}
<hr/>
&copy; 2018
</body>
</html>
@whatsdis
Copy link

감사합니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment