【发布时间】:2021-01-19 20:39:33
【问题描述】:
我有以下 Django 视图方法,用于将信息发送到 details.html 中的一些 Vue.js 前端代码。我基本上想知道如何将数据发送到 vue 模板。如何进行 JSON 转储?我想这就是我在这里搞砸的。
def works(request):
heading_info = Heading.objects.order_by('-name')[:1]
welcome_info = Welcome.objects.order_by('-title')[:1]
skills_info = Skill.objects.order_by('position')[:5]
projects_info = Project.objects.order_by('name')[:10]
items = []
items.append({'heading_info':heading_info, 'welcome_info':welcome_info, 'path':path, 'path2':path2, 'skills_info':skills_info,'projects_info':projects_info})
# context = {}
# context["items_json"] = json.dumps(items)
context = {'heading_info':heading_info, 'welcome_info':welcome_info, 'path':path, 'path2':path2, 'skills_info':skills_info,'projects_info':projects_info}
return render(request, 'home/details.html', context)
这是我试图访问这些数据的 html。
<script type='text/javascript'>
var data = {{ projects_info|safe }};
</script>
<div id="app">
[[projects_info_vue]]
{% comment %} #Passing array as vue data. This gets rendered as QuerySet. How do I access these value in Vue. {% endcomment %}
<div class="row">
{% for project in projects_info %}
{% comment %} #Here is the array being rednered in Django. This array gets rendered as a QuerySet in Vue. {% endcomment %}
<div class="col">
{{project.name}}
</div>
{% endfor %}
</div>
</div>
<script>
var app = new Vue({
delimiters: ["[[", "]]"],
el: '#app',
data: {
projects_info_vue: '{{projects_info}}',
},
});
</script>
【问题讨论】: