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

效果:

相关推荐
lly2024062 分钟前
Python SMTP邮件发送教程
开发语言
我是伪码农7 分钟前
小程序100-125
开发语言·小程序·php
weixin_4467291616 分钟前
注解和反射
java·开发语言
alphaTao17 分钟前
LeetCode 每日一题 2026/5/18-2026/5/24
python·leetcode
徐安安_ye119 分钟前
FlashAttention学习路线:从调API到写算子,你该走哪条路
python·学习
এ慕ོ冬℘゜22 分钟前
JS 前端基础高频面试题
开发语言·前端·javascript
凯瑟琳.奥古斯特24 分钟前
常见加密算法及应用
java·开发语言·网络·网络协议·职场和发展
Dxy123931021627 分钟前
JS列表获取指定范围值的 N 种方法
开发语言·javascript·ecmascript
froginwe1129 分钟前
Memcached CAS 命令详解
开发语言
IT策士37 分钟前
Django 从 0 到 1 打造完整电商平台:商品搜索
后端·python·django