파이썬에서 세션이나 데이터를
자바스크립트와 HTML에 뿌리는방법으로는 Jinja를 활용하면된다.
준비
customized_id = session.get(const.ID)
if customized_id != None:
return render_template('index.html')
return render_template('index.html', customized_id = customized_id)
renderTemplate에서 customized_id = customized_id처럼 넘겨줘야 HTML템플릿내에서 jinja를 활용할수있다.
두개이상은 튜플로 넘겨줄수있는것같다.
HTML에서 세션가져오기
<input id="example" value="{{session['id']}}" hidden>
<input id="example2" value="{{ id }}" hidden>
자바스크립트에서 세션가져오기
파라메터로 가져오려면
<a href="javascript:down_visitors('{{example_id}}')"><span>테스트</span></a>
<script>down_visitors('{{example_id}}')</script>
변수로 가져오려면
let example = '{{ example }}'//문자열
let example = {{ example }}//숫자? 이건확실치않음
*if문
{% if session['id'] == '' %}
<span>id없음</span>
{% else %}
<span>id있음</span>
{% endif %}
https://stackoverflow.com/questions/21626048/unable-to-pass-jinja2-variables-into-javascript-snippet
Unable to pass jinja2 variables into javascript snippet
How do i pass jinja2 data into javascript. I have a Flask REST url as /logs/<test_case_name> I am trying use .getJSON() to query the above URL and hence would want to pass the jinja2 data wh...
stackoverflow.com
반응형
'Backend' 카테고리의 다른 글
| python dictionary 예제 (0) | 2021.11.29 |
|---|---|
| 파이썬 controller 관련 지식 총정리(args) (0) | 2021.11.22 |
| 파이썬 삼항연산자 예시, cannot assign to conditional expression (0) | 2021.11.04 |
| [flask 회원1] 테이블만들기 (0) | 2021.07.16 |
| [flask 기초2] SQLAlchemy (0) | 2021.07.16 |