luffy-03

2023-05-16

这里写目录标题

  • 一.昨日回顾
  • 二.今日内容
    • 1.跨域问题
      • 1.1后端自己处理跨域
        • 简单请求
        • 非简单请求
        • 中间件处理
      • 1.2前端处理跨域
        • App.vue
        • vue.config.js
    • 2.头部组件,尾部组件
      • components/Header.vue
      • components/Footer.vue
    • 3.首页组件,轮播图组件
      • components/Banner.vue
      • views/Home.vue
      • views/ActualCourse.vue
      • views/FreeCourse.vue
      • views/LightCourse.vue
      • App.vue
      • router/index.js
    • 4.git入门

一.昨日回顾

1 前端配置
	-cnpm install jQuery --save # 依赖模块同步到package.json中
	-vue create 项目名字(eslint不使用,vuex,router,bable)
	-npm run serve
	-npm run build  #项目上线,执行,把dist文件夹部署,前端所有的代码
	-每个组件都有三部分
		-template
		-script
		-style
2 后端配置
	-全局异常
	-全局响应对象Response对象
	-记录日志
3 跨域问题解决
	-前端使用代理(什么叫反向代理,什么是正向代理)
	-自己处理(CORS:跨资源共享)(csrf:跨站请求伪造,xss:跨站脚本攻击)
	-借助第三方 django-cors-headers
		-注册app
		-写中间件
		-一堆配置

二.今日内容

1.跨域问题

1 同源策略:浏览器的安全策略,不允许去另一个域加载数据
2:ip或者端口都必须一致
3 前后端分离项目会出现跨域
4 不使用第三方,自己处理
5 CORS:后端技术,跨域资源共享,服务端只要做配置,(本身浏览器已经支持了),就支持跨域
	-access-control-allow-origin: *  #所有的域都可以向我发送请求,浏览器不会禁止
6 浏览器将CORS请求分成两类:
	-简单请求(simple request)
	-非简单请求(not-so-simple request)
7 满足以下两大条件就是简单请求
	(1)请求方法是以下三种方法之一:
		HEAD
		GET
		POST
	(2)HTTP的头信息不超出以下几种字段
		Accept
        Accept-Language
        Content-Language
        Last-Event-ID
        Content-Type:只限于三个值application/x-www-form-urlencoded、multipart/form-data、text/plain
8 简单请求,只发送一次,如果后端写了CORS,浏览器就不禁止了
9 非简单请求,发两次,第一次是OPTIONS(预检请求),看后端是否允许,如果允许再发真正的请求

1.1后端自己处理跨域

简单请求

##### 1.原django项目:apps/user/views.py
from django.http import JsonResponse

def test(request):
	obj=JsonResponse({'name':'lqz','age':18})
	# 针对简单请求
	obj['Access-Control-Allow-Origin']='*' #允许所有ip访问
	return obj
##### 2.原django项目:apps/user/urls.py
from django.urls import path
from user import views
urlpatterns = [
	path('test/',views.test),

]
##### 3.在创建一个django项目(用其他端口号)
###### views.py
from django.shortcuts import render
def index(request):
    return render(request, 'index.html')
###### urls.py
from django.urls import path
from app01 import views

urlpatterns = [
    path('test/', views.index),
]
<!--templates中建一个index.html-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <title>Title</title>
</head>
<body>
<button id="btn">点我</button>

</body>
<script>
    $('#btn').click(function(){
        $.ajax({
            url:'http://127.0.0.1:8000/user/test/',
            method:'get',
            success:function(data){
                console.log(data)
            }
        })
    })
</script>
</html>

在这里插入图片描述

非简单请求

原django项目apps/user/views.py

from django.http import JsonResponse


def test(request):
    obj = JsonResponse({'name': 'Lisa', 'age': '18'})
    if request.method == 'OPTIONS':
        obj['Access-Control-Allow-Headers'] = 'Content-Type,authorization'
        # obj['Access-Control-Allow-Headers'] = '*'
    obj['Access-Control-Allow-Origin'] = 'http://127.0.0.1:8001'
    # obj['Access-Control-Allow-Origin'] = '*'
    return obj

新django项目templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<button id="btn">点我</button>

</body>
<script>
    $('#btn').click(function () {
        let obj = {name: 'Lisa'}
        $.ajax({
            url: 'http://127.0.0.1:8000/user/test/',
            method: 'post',
            contentType: 'application/json',
            headers: {authorization: 'jwt'},
            data: JSON.stringify(obj),
            success: function (data) {
                console.log(data)
            }
        })
    })
</script>
</html>

中间件处理

在原django项目的根路径创建middle.py

# 1.写一个中间件
from django.utils.deprecation import MiddlewareMixin
class CoreMiddle(MiddlewareMixin):
    def process_response(self, request, response):
        if request.method == 'OPTIONS':  # 处理了非简单请求
            response['Access-Control-Allow-Headers'] = 'Content-Type,authorization'
        # 处理了简单请求
        response['Access-Control-Allow-Origin'] = '*'
        return response

# 2.setting中注册
MIDDLEWARE = [
	...
    'middle.CoreMiddle', # 自己写的处理跨域问题的中间件
    ...
]
# 3.setting中注释掉中间件中的csrf
# 4.原django项目apps/user/views中替换成如下代码
from django.http import JsonResponse
def test(request):
    return JsonResponse({'name': 'lqz', 'age': '18'})

在这里插入图片描述

1.2前端处理跨域

App.vue

<template>
  <div id="app">
    <h1>我是主页</h1>
    <div v-html="ret"></div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      ret: ''
    }
  },
  mounted () {
    // this.$axios.get('http://127.0.0.1:8000/user/test/')
    this.$axios.get('/asgard/app/video/').then(res => {
      // console.log(res.data)
      this.ret = res.data
    })
  }
}
</script>

vue.config.js

const webpack = require('webpack')

module.exports = {
  devServer: {
    proxy: {
      '/asgard': {
        target: 'https://m.maoyan.com/',
        changeOrigin: true
      },
      '/user': {
        target: 'http://127.0.0.1:8000/',
        changeOrigin: true
      }

    }
  }
}

在这里插入图片描述

在这里插入图片描述

2.头部组件,尾部组件

components/Header.vue

<template>
    <div class="header">
        <div class="slogan">
            <p>老男孩IT教育 | 帮助有志向的年轻人通过努力学习获得体面的工作和生活</p>
        </div>
        <div class="nav">
            <ul class="left-part">
                <li class="logo">
                    <router-link to="/">
                        <img src="../assets/img/head-logo.svg" alt="">
                    </router-link>
                </li>
                <li class="ele">
                    <span @click="goPage('/free-course')" :class="{active: url_path === '/free-course'}">免费课</span>
                </li>
                <li class="ele">
                    <span @click="goPage('/actual-course')" :class="{active: url_path === '/actual-course'}">实战课</span>
                </li>
                <li class="ele">
                    <span @click="goPage('/light-course')" :class="{active: url_path === '/light-course'}">轻课</span>
                </li>
            </ul>

            <div class="right-part">
                <div>
                    <span>登录</span>
                    <span class="line">|</span>
                    <span>注册</span>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Header",
        data() {
            return {
                //sessionStorage中有url_path就使用它,没有就是 /
                url_path: sessionStorage.url_path || '/',
            }
        },
        methods: {
            goPage(url_path) {
                // 已经是当前路由就没有必要重新跳转
                if (this.url_path !== url_path) {
                    //js控制路由跳转
                    this.$router.push(url_path);
                }
                //把当前路径加入到了sessionStorage
                sessionStorage.url_path = url_path;
            },
        },
        // created() {
        //     sessionStorage.url_path = this.$route.path;
        //     this.url_path = this.$route.path;
        // }
    }
</script>

<style scoped>
    .header {
        background-color: white;
        box-shadow: 0 0 5px 0 #aaa;
    }

    .header:after {
        content: "";
        display: block;
        clear: both;
    }

    .slogan {
        background-color: #eee;
        height: 40px;
    }

    .slogan p {
        width: 1200px;
        margin: 0 auto;
        color: #aaa;
        font-size: 13px;
        line-height: 40px;
    }

    .nav {
        background-color: white;
        user-select: none;
        width: 1200px;
        margin: 0 auto;

    }

    .nav ul {
        padding: 15px 0;
        float: left;
    }

    .nav ul:after {
        clear: both;
        content: '';
        display: block;
    }

    .nav ul li {
        float: left;
    }

    .logo {
        margin-right: 20px;
    }

    .ele {
        margin: 0 20px;
    }

    .ele span {
        display: block;
        font: 15px/36px '微软雅黑';
        border-bottom: 2px solid transparent;
        cursor: pointer;
    }

    .ele span:hover {
        border-bottom-color: orange;
    }

    .ele span.active {
        color: orange;
        border-bottom-color: orange;
    }

    .right-part {
        float: right;
    }

    .right-part .line {
        margin: 0 10px;
    }

    .right-part span {
        line-height: 68px;
        cursor: pointer;
    }
</style>

components/Footer.vue

<template>
    <div class="footer">
        <ul>
            <li>关于我们</li>
            <li>联系我们</li>
            <li>商务合作</li>
            <li>帮助中心</li>
            <li>意见反馈</li>
            <li>新手指南</li>
        </ul>
        <p>Copyright © luffycity.com版权所有 | 京ICP备17072161号-1</p>
    </div>
</template>

<script>
    export default {
        name: "Footer"
    }
</script>

<style scoped>
    .footer {
        width: 100%;
        height: 128px;
        background: #25292e;
        color: #fff;
    }

    .footer ul {
        margin: 0 auto 16px;
        padding-top: 38px;
        width: 810px;
    }

    .footer ul li {
        float: left;
        width: 112px;
        margin: 0 10px;
        text-align: center;
        font-size: 14px;
    }

    .footer ul::after {
        content: "";
        display: block;
        clear: both;
    }

    .footer p {
        text-align: center;
        font-size: 12px;
    }
</style>

3.首页组件,轮播图组件

components/Banner.vue

<template>
    <div>
        <el-carousel height="400px">
            <el-carousel-item v-for="item in 4" :key="item">
                <img src="../assets/img/banner4.png" alt="">
            </el-carousel-item>
        </el-carousel>
    </div>
</template>

<script>
    export default {
        name: "Banner",

    }
</script>

<style scoped>

.el-carousel__item {
        height: 400px;
        min-width: 1200px;
    }
    .el-carousel__item img {
        height: 400px;
        margin-left: calc(50% - 1920px / 2);
    }
</style>

views/Home.vue

<template>
    <div id="home">
        <Header></Header>
        <Banner></Banner>
        <br><br><br><br><br><br>
        <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
        内容部分
        <br><br><br><br>
        <Footer></Footer>
    </div>
</template>

<script>

    import Header from "../components/Header";
    import Footer from "../components/Footer";
    import Banner from "../components/Banner";
    export default {
        name: "Home",
        data(){
            return {

            }
        },
        components:{
            Header,
            Footer,
            Banner
        }
    }
</script>

<style scoped>

</style>

views/ActualCourse.vue

<template>
  <div>
    <Header></Header>
    <h1>实战课</h1>
  </div>
</template>

<script>
import Header from '../components/Header'
export default {
  name: 'ActualCourse',
  data () {
    return {
      currentDate: new Date()
    }
  },
  components: {
    Header
  }
}
</script>

<style scoped>

</style>


views/FreeCourse.vue

<template>
    <div>
      <Header></Header>
      <h1>免费课</h1>
    </div>
</template>

<script>
import Header from '../components/Header'

export default {
  name: 'FreeCourse',
  data () {
    return {
      currentDate: new Date()
    }
  },
  components: {
    Header
  }
}
</script>

<style scoped>

</style>

views/LightCourse.vue

<template>
  <div>
    <Header></Header>
    <h1>轻课</h1>
  </div>
</template>

<script>
import Header from '../components/Header'

export default {
  name: 'LightCourse',
  data () {
    return {
      currentDate: new Date()
    }
  },
  components: {
    Header
  }
}
</script>

<style scoped>

</style>

App.vue

<template>
  <div id="app">
    <router-view/>
  </div>
</template>

<script>

</script>

router/index.js

import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import ActualCourse from '../views/ActualCourse.vue'
import LightCourse from '../views/LightCourse.vue'
import FreeCourse from '../views/FreeCourse.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/actual-course',
    name: 'ActualCourse',
    component: ActualCourse
  },
  {
    path: '/light-course',
    name: 'LightCourse',
    component: LightCourse
  },
  {
    path: '/free-course',
    name: 'FreeCourse',
    component: FreeCourse
  }
]

const router = new VueRouter({
  routes
})

export default router

4.git入门

1 协同开发:版本管理工具
	-svn
	-git
2 git能干什么
	完成协同开发项目,帮助程序员整合代码
		i)帮助开发者合并开发的代码
        ii)如果出现冲突代码的合并,会提示后提交合并代码的开发者,让其解决冲突
软件:SVN 、 GIT(都是同一个人的个人项目)
        github、gitee(两个采用git版本控制器管理代码的公共平台)
        github,gitee,gitlab:区别
        	-github:一般开源的代码放再github,代码托管平台(公有仓库,私有仓库),公司代码不会放				在这上面
            -gitee(码云):中国的github,开源代码放在共有仓库,有一部分公司的公司代码托管到gitee				的私有仓库(你们公司代码放在码云上)
            -gitlab:公司自己的搭建github,公司内部自己访问(docker拉一个gitlab镜像,跑起来即可)

    git:集群化、多分支
    
3 安装git客户端
	一路下一步
4 右键---》git bash(terminal窗口)
	git init  :初始化仓库	
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

luffy-03 的相关文章

  • vue中methods、mounted等使用方法整理

    created html加载完成之前 xff0c 执行 执行顺序 父组件 子组件 mounted html加载完成后执行 执行顺序 子组件 父组件 methods 事件方法执行 watch 去监听一个值的变化 xff0c 然后执行相对应的函
  • 使用STM32G4 ----串口发送数据USART_TX的使用

    一 引脚的配置 串口对应的引脚 PA10 span class token operator span span class token operator span span class token operator span span c
  • 【MFC】CListCtrl控件的GetItem用法

    GetItem 方法 说明 检索列表视图项的部分属性或全部属性 BOOL GetItem LVITEM pItem const 参数 pItem 指向 LVITEM 结构的指针 xff0c 该结构接收项的属性 返回值 如果成功 xff0c

随机推荐

  • NVIDIA Jetson Xavier NX部署VINS-fusion-GPU

    NVIDIA Jetson Xavier NX部署VINS fusion GPU 一 环境配置 xff08 Ubuntu 18 04 xff09 1 Cuda 10 2的安装 span class token function sudo s
  • OPi5 香橙派5 安卓12 隐藏大屏模式下任务栏

    记录一下 原版安卓系统在检测到副屏 xff0c 会进入大屏模式 xff0c 屏幕底下有个难看又占地方的任务栏 以下是隐藏的方法 xff0c 在所有界面都会隐藏 xff0c 包括三个虚拟按键 不过 xff0c 用其他APP仍然可以调用包括最近
  • 无人机飞行控制基础

    坐标系统 描述无人机的运动依赖于无人机的位置以及它的方向 常见的主要有两种坐标系统 xff1a 无人机坐标系和大地坐标系 无人机坐标系 无人机坐标系是相对于无人机自身的坐标系统 如下图 坐标原点位于无人机质心 xff0c 三个坐标轴互相垂直
  • socketcan_bridge 包 设置多路CAN口 小记

    参考链接 xff1a socketcan bridge ROS Wiki 基于ROS 43 CANopen的SocketCAN驱动在Ubuntu下的应用说明 SzZhangfq的博客 CSDN博客 ros socketcan socketc
  • 你知道几个中文编程语言,快来瞧瞧这些有趣的中文编程语言。

    提到编程语言 xff0c 我们所了解的也是比较广为人知的一些主流编程语言 xff0c 如Java C C 43 43 Python PHP等 那除了这些编程语言 xff0c 你有了解过中文编程语言吗 xff1f 如果没有 xff0c 那今天
  • Win10连接无线“需要执行操作”或无网络问题的解决方法。

    转载自品略图书馆 http www pinlue com article 2020 03 2800 3510060445349 html 最近这几天有用户反馈Win10系统连接网络出现异常的问题 xff0c 表现为连接到无线网络WIFI之后
  • Excel怎么比较两列文本是否相同?

    转载自品略图书馆 http www pinlue com article 2020 05 2215 1410586873210 html 这个问题很简单 xff0c 属于Excel基础操作技巧 xff0c 我就分享我最喜欢用的三招给你 xf
  • 为什么电脑唯独搜不到自己家wifi?

    转载自品略图书馆 http www pinlue com article 2020 05 2213 2410586244619 html 电脑唯独搜不到自己家wifi xff0c 别人家的都能搜到 xff0c 手机也可以搜到自己家的 xff
  • http://和www.前缀网站有什么具体区别?

    转载自品略图书馆 http www pinlue com article 2019 03 1813 598231572617 html 将http 和www 放一起比较 xff0c 是没有实际意义的 一 http协议二 域名一 http协议
  • 什么是三层交换机、网关、DNS、子网掩码、MAC地址?

    转载自品略图书馆 http www pinlue com article 2020 08 2313 2511146576256 html 一文讲懂什么是三层交换机 网关 DNS 子网掩码 MAC地址 很多朋友多次问到什么是网关 dns 子网
  • C++类对象共享数据的5种实现方法

    转自 xff1a http www pinlue com article 2020 09 2617 0611262487540 html
  • c语言free的用法

    转自 xff1a http www pinlue com article 2020 03 3100 4610073901713 html
  • Spring Boot 修改默认端口号

    修改配置文件 xff0c 加上参数 xff1a server port 61 8014 或者 xff1a server port 8014 启动后可发现tomcat运行在端口8014上了 实现原因可看以下链接 转载 SpringBoot修改
  • php调用类中的方法

    转自 xff1a http www pinlue com article 2020 06 1219 0410725563037 html
  • 人工智能 : 第三篇”脑机接口“

    本文作者Tim Urban xff1a Wait but Why的作者Tim Urban 是埃隆马斯克 xff08 特斯拉 SpaceX创始人 xff09 强烈推荐的科技博主 他写的AI文章是全世界转发量最高的 他的粉丝还包括 xff1a
  • 如何找回一台丢失的Win10电脑?

    今天说说如何找电脑 为什么小微想到了这个问题 还要从一次关于奇葩办公地点的讨论说起 看到大家的回答 xff0c 小微佩服得五体投地 办公经历还可以如此精jing彩xin绝dong伦po 作为结实靠谱的出行伙伴 ThinkPad陪伴大家出现在
  • 不必再狂按空格键了!Word 里文字对齐推荐这4种方法

    我们在用Word写论文 制作简历的时候 xff0c 通常会遇到把word中某些特定文字对齐的情况 那么问题来了 xff0c 你平时都是怎么对齐文字的 xff1f 傻傻的用空格来对齐吗 xff1f 在字符数不等的情况下 xff0c 加空格不仅
  • AMI主板BIOS菜单图文讲解设置!

    电脑硬件 xff0c 包括电脑中所有物理的零件 xff0c 以此来区分它所包括或执行的数据和为硬件提供指令以完成任务的软件 主要包含 机箱 xff0c 主板 xff0c 总线 xff0c 电源 xff0c 硬盘 xff0c 存储控制器 xf
  • luffy-02

    这里写目录标题 一 昨日回顾二 今日内容1 路飞前台配置 1 重构项目目录 2 文件修订 xff1a 目录中非配置文件的多余文件可以移除router的使用 3 前台配置 全局样式 配置文件 axios vue cookies element
  • luffy-03

    这里写目录标题 一 昨日回顾二 今日内容1 跨域问题1 1后端自己处理跨域简单请求非简单请求中间件处理 1 2前端处理跨域App vuevue config js 2 头部组件 尾部组件components Header vuecompon