본문 바로가기
프로그래밍(Programming)/장고(Django)

[Django] 템플릿 변수 스크립트 태그에서 사용하는 방법 | How to use Template variable in Script tag

by 갈팡이지팡이 2019. 10. 2.

 

"본 글은 '2019-10-02' 최초 작성되었으며, 업데이트 발생 시 글 내용 일부가 수정될 수 있음"

들어가기

    사건 경위

...더보기

장고 템플릿 언어를 Template 구역의 <script> 태그 안에서 사용하고 싶었음

이 짓 저 짓 다해봤는데, 안되길래 포기할 까 하다가 혹시나 이 방법은 될까? 싶어서

해봤는데 됬음;;


Step 1. "" 사용하기

1.1. html 파일 수정

...더보기

Write source code like example.html in your html file

<!-- example.html -->
<script type="text/javascript">
    function function_name(){
        var var_name = "{{ something }}";
        alert(var_name);
    }
</script>
<form onsubmit="return function_name()">
    <input type="submit">
</form>

 

My source code

<!-- My source code -->
<script type="text/javascript">
    var username = "{{ user.username }}";
        function check_same_value(){
        alert(username);
    }
</script>
<form ~ onsubmit="return check_same_value()">
    <input type="submit">
</form>

 

 

 


나오기

    느낀 점

...더보기

이게 수정 전 코드인데, 

<!-- My source code -->
<script type="text/javascript">
    var username = {{ user.username }};
        function check_same_value(){
        alert(username);
    }
</script>
<form ~ onsubmit="return check_same_value()">
    <input type="submit">
</form>

왜 안됨? 왜 안되지? 아니 뭔데? 도대체 왜 때문임? 뭐임?

 

↓ 이거(공감) 눌러주면 진짜 힘남!