Version
Django 2.2.3
Python 3.8.8
djangorestframework 3.13.1
django-cors-headers 3.11.0
django implements cross-domain
Note: This method is back-end solution for leap, that is, django solution for leap.
1. Installationdjango-cors-headers
Library
pip install django-cors-headers
2. Modify the project configuration fileproject/
... # Application definition INSTALLED_APPS = [ '', 'corsheaders', # Add: Cross-domain components 'rest_framework', # Add: DRF Framework 'home', # Sub-app] MIDDLEWARE = [ '', # Add: Put the first line (put other lines not tested) '', ... # CORS group configuration informationCORS_ORIGIN_WHITELIST = ( 'http://127.0.0.1:8080', # Note here: 1. You must add http:// or else an error will be reported (https not tested) 2. This address is the cross-domain address, that is, the front-end address) CORS_ALLOW_CREDENTIALS = True # allowajaxCarry it when requesting across domainscookie
Django configuration is now complete
3. Front-end vue useaxios
Access the data interface provided by the backend django and install itaxios
npm install axios -S
4. Front-end vue configurationaxios
Plugin, modifysrc/
... import axios from 'axios'; // Add: Import axios package // = true; // Cookies are included when ajax is allowed to send requests.$axios = axios; // Mount the object in vue···
5. In
Mid-span domain request data
··· created: function() { // Get the carousel image this.$("http://127.0.0.1:8000/book/").then(response => { () this.banner_list = }).catch(response => { (response) }) // http://127.0.0.1:8000/book/ This is the backend django interface···
Code
<template> <div class="div1"> <el-carousel trigger="click" height="600px"> <el-carousel-item v-for="book in book_list" :key=""> <a :href="" rel="external nofollow" > <img width="80%" :src="" alt=""> </a> </el-carousel-item> </el-carousel> </div> </template> <script> export default { name:"Book", data(){ return { book_list:[] }; }, created: function() { // Get the carousel image this.$("http://127.0.0.1:8000/book/").then(response => { () this.book_list = }).catch(response => { (response) }) } } </script> <style> .div1 { margin-top: 100px; height: 1000px } img { width: auto; height: auto; max-width: 100%; max-height: 100%; </style>
This is the end of this article about django+vue cross-domain implementation. For more related django vue cross-domain content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!