반응형
Jinja 템플릿을 사용하는 내 서버에 Flask 마이크로 프레임 워크를 사용하고 있습니다.
부모 template.html
과 child1.html
및 child2.html
이라는 하위 템플릿이 있습니다. 이러한 하위 템플릿 중 일부는 매우 큰 HTML입니다. 내 작업에 대한 더 나은 명료성을 위해 어떻게 든 분할하고 싶습니다.
내 main.py
스크립트의 내용 :
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/')
@app.route('/<task>')
def home(task=''):
return render_template('child1.html', task=task)
app.run()
단순화 된 template.html
:
<!DOCTYPE html>
<html>
<head></head>
<body>
<div class="container">
{% block content %}{% endblock %}
</div>
</body>
</html>
마법은 child1.html
에 있습니다.
{% extends 'template.html' %}
{% block content %}
{% if task == 'content1' %}
<!-- include content1.html -->
{% endif %}
{% if task == 'content2' %}
<!-- include content2.html -->
{% endif %}
{% endblock %}
댓글 대신 :
<!-- include content1.html -->
나는 많은 html 텍스트를 가지고 있으며 변경 사항을 추적하고 실수하지 않는 것이 매우 어렵습니다.
child1.html
에 모두 작성하는 대신 content1.html
만로드하고 싶습니다.
Jinja2에 더 나은 도구가있을 수 있다고 생각합니다.
참고 : 위 코드는 제대로 작동하지 않을 수 있으며 문제를 설명하기 위해 작성한 것입니다.
해결 방법
{% extends 'template.html' %}
{% block content %}
{% if task == 'content1' %}
{% include 'content1.html' %}
{% endif %}
{% if task == 'content2' %}
{% include 'content2.html' %}
{% endif %}
{% endblock %}
여기에는 올바른 콘텐츠 파일의 콘텐츠가 포함됩니다.
참조 페이지 https://stackoverflow.com/questions/22860085
반응형
'파이썬' 카테고리의 다른 글
파이썬 (python) [Errno 11001] getaddrinfo 실패 (0) | 2020.12.17 |
---|---|
파이썬 문자열을 문자열 집합에 매핑하는 Python 사전? (0) | 2020.12.17 |
파이썬 Git Bash가 내 파이썬 파일을 실행하지 않습니까? (0) | 2020.12.16 |
파이썬 Python에서 브로드 캐스트 패킷 받기 (0) | 2020.12.16 |
파이썬 ImportError : 'pymongo'라는 모듈이 없습니다. (0) | 2020.12.16 |
댓글