A simple example.
views.py
def articles(request): context { 'articles' : ['a1','a2','a3'] } return render(request, 'articles.html', context)
articles.html
{% verbatim %} <div id="app"> <ul> <li v-for="a in articles">{{ a }}</li> </ul> </div> {% endverbatim %} <script> new Vue({ el : "#app", data : function(){ return { articles : {{ articles | safe }} } } }) </script>
Beware:
- The
verbatim tag to stop Django from displaying the contents of this block tag, as Vue uses the same interpolating characters. safe filter to prevent Django content from spreading.- If you are going through a dictionary, consider including it in JSON first.
Generally speaking, prefer to pass data to Vue through Ajax
zxzak source share