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

效果:

相关推荐
大鱼>4 分钟前
DSPy:LLM程序自动编译与提示词优化
开发语言·人工智能·python·深度学习
2401_843253709 分钟前
金融智能:AI如何重构银行业未来
人工智能·python·金融
uncle_ll11 分钟前
服务器选型、微调范式、训练优化与环境搭建
服务器·python·gpt·llm·nlp
撩妹帝九歌12 分钟前
Java文件写入与编码、字节数组、字符集、字符编解码 一文打通!
java·开发语言
A242073493014 分钟前
Vue.js 初学者注意事项与项目开发实践指南
前端·javascript·vue.js
星核0penstarry20 分钟前
Solon AI v4.0.4 技术分析:Java Agent 框架的兼容性突破与选型考量 基于 OSCHINA 2026-07-30 报道及公开技术资料整理
java·开发语言·人工智能
昨夜星河入梦来27 分钟前
postman接口测试报错503的解决方法
开发语言·postman
久久学姐30 分钟前
Python开发爬虫的常用技术架构
爬虫·python·http·框架·数据存储
circuitsosk30 分钟前
大规模离线数据管道构建:样本获取、清洗、加工与合成
人工智能·python·机器学习·搜索引擎
tang777891 小时前
分布式爬虫优化指南:如何用代理IP把采集效率提升300%
分布式·爬虫·python·tcp/ip·分布式爬虫·爬虫代理·代理ip