Staticfile CDN(國內):css
unpkg:會保持和npm發佈的最新的版本一致前端
cdnjs:node
https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.8/vue.min.jsios
在new Vue設置: `delimiters:['[[', ']]']`, 而後在html使用 `[[ ]]` 代替 `{{}}`
使用 `{% verbatim myblock %} {% endverbatim myblock %}` 包裹vue裏面的`{{}}`, 此標籤包裹的代碼將不會被Django的模板引擎渲染
django服務端代碼:ajax
def vue_views(request): List = [{'age':18},200] OBJ = {"name":"隔壁老王"} dic = { 'list':json.dumps(List), 'obj':json.dumps(OBJ) } return render(request,'Vue.html',dic)
Vue前端代碼:npm
<script> var list = {{ list | safe }} var obj = {{ obj | safe }} console.log(list,obj) </script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script> <script> axios.get('/newmodle?a=1&b=2') .then(function (response) { console.log(response) console.log(response.data) }) </script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script> <script src="https://cdn.bootcss.com/qs/6.5.1/qs.min.js"></script> <script> function getCookie (name) { var value = '; ' + document.cookie var parts = value.split('; ' + name + '=') if (parts.length === 2) return parts.pop().split(';').shift() } axios({ url: '/newmodle/', method: 'post', responseType: 'json', // 默認的 data: Qs.stringify({ 'a': 1, 'b': 2, }), headers: { 'X-CSRFToken': this.getCookie('csrftoken') } }) .then(function (response) { console.log(response) console.log(response.data) }) </script>
使用全局對象方式 Vue.http 或者在一個 Vue 實例的內部使用 this.$http來發起 HTTP 請求django
Vue 要實現異步加載須要使用到 vue-resource 庫json
<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
this.$http.get('/user/newmodle?a=1&b=2') .then(function(response){ console.log(response); console.log(response.data) }
<script src="/static/vue.min.js"></script> <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script> <script src="https://cdn.bootcss.com/qs/6.5.1/qs.min.js"></script> <script> function getCookie (name) { var value = '; ' + document.cookie var parts = value.split('; ' + name + '=') if (parts.length === 2) return parts.pop().split(';').shift() } var app = new Vue({ el : '#app', data : { text:'' }, mounted:function(){ this.$http({ url: '/newmodle', method: 'post', responseType: 'json', data: Qs.stringify({ 'a': 1, 'b': 2, }), headers: {'X-CSRFToken': getCookie('csrftoken')} }) .then(function (response) { console.log(response); console.log(response.data); }) } }) </script>
Ajax
Ajax 即「Asynchronous Javascript And XML」,是指一種建立交互式網頁應用的網頁開發技術
axios:
用於瀏覽器和node.js的基於Promise的HTTP客戶端