Python 前后端分离项目Vue部署应用

一、视图创建

python 复制代码
from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views import View


class IndexView(View):
    def get(self,request):

        # 前后端分离 (前端JS代码渲染数据)
        return JsonResponse({'name':'Hello,mike'})

二、配置子路由

python 复制代码
from django.urls import path

from book import views

urlpatterns = [
    #子路由
    path('', views.IndexView.as_view()),

]

三、配置主路由

python 复制代码
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('book.urls')),  #子路由
    path('admin/', admin.site.urls),
]

四、解决跨域查看(Python 安装django-cors-headers解决跨域问题-CSDN博客

五、HTML页面

my.html页面

python 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>前后端分离数据</title>
    <script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
    <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">{{name}}</div>
<script>
    new Vue({
        el:'#app',
        data:{
            name:'',
        },
        mounted:function () {
           axios.get('http://127.0.0.1:8000')
            .then(response=>{
                this.name=response.data.name
            })
            .catch(error=>{
                alert(error)
            })
        },
    })
</script>
</body>
</html>

六、运行前后端服务

http://127.0.0.1:8000

http://127.0.0.1:8080/my.html

效果:

相关推荐
qwerasda1238521 分钟前
车牌字符识别与定位技术:多国家车牌检测与识别系统
python
筱歌儿1 分钟前
TinyMCE-----word表格图片进阶版
开发语言·javascript·word
独行soc5 分钟前
2026年渗透测试面试题总结-3(题目+回答)
网络·python·安全·web安全·渗透测试
黎雁·泠崖10 分钟前
Java面向对象:对象数组进阶实战
java·开发语言
sg_knight16 分钟前
工厂方法模式(Factory Method)
java·服务器·python·设计模式·工厂方法模式·工厂模式
%xiao Q25 分钟前
GESP C++四级-216
java·开发语言·c++
西红市杰出青年26 分钟前
Python异步----------信号量
开发语言·python
tianyuanwo30 分钟前
深入浅出SWIG:从C/C++到Python的无缝桥梁
c语言·c++·python·swig
金融小白数据分析之路40 分钟前
msoffcrypto-tool库 Excel 加密
python·excel
程序员敲代码吗1 小时前
使用Python进行PDF文件的处理与操作
jvm·数据库·python