0
1

DjangoテンプレートでJSに値を渡す

Last updated at Posted at 2024-04-16

個人的なメモ

view.py
def index(request):
  context = {
    'data': 'a',
    'data_array': ['b', 'c', 'd'],
    'data_dict': {'key': 'e'}
  }
  return render(request, 'index.html', context)
index.html
<script>
  const data = "{{ data }}";
  const dataAry = {{ data_array|safe }};
  const dataObj = {{ data_dict|safe }};

  console.log('data:', data);
  console.log('dataAry:', dataAry);
  console.log('dataObj:', dataObj);
</script>

コンソール

data: a
dataAry: (3) ['b', 'c', 'd']
dataObj: {key: 'e'}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1