Python正課152 —— luffy專案 頭部、輪播圖、介面
阿新 • • 發佈:2020-07-20
本文內容皆為作者原創,如需轉載,請註明出處:https://www.cnblogs.com/xuexianqi/p/13347573.html
一:路由跳轉
1.html中路由跳轉
<router-link to="/">
<img src="../assets/img/head-logo.svg" alt="">
</router-link>
2.js中控制路由跳轉
this.$router.push('/');
二:Vue專案(luffycity)
1.頭部Header元件
在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 { url_path: sessionStorage.url_path || '/', } }, methods: { goPage(url_path) { // 傳入的路由如果不是當前所在路徑,就跳轉 if (this.url_path !== url_path) { this.$router.push(url_path); } 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>
2.配置全域性css和`setting
在main.js
中配置
// 配置全域性樣式 @ 符號,代指src路徑
import '@/assets/css/global.css'
// 配置全域性自定義設定
import settings from '@/assets/js/settings'
Vue.prototype.$settings = settings;
// 在所有需要與後臺互動的元件中:this.$settings.base_url + '再拼接具體後臺路由'
3.在assets
下的css
資料夾中加入global.css
先提前在assets
中 建立css
、js
、img
3個資料夾
/* 宣告全域性樣式和專案的初始化樣式 */ body, h1, h2, h3, h4, h5, h6, p, table, tr, td, ul, li, a, form, input, select, option, textarea { margin: 0; padding: 0; font-size: 15px; } a { text-decoration: none; color: #333; } ul { list-style: none; } table { border-collapse: collapse; /* 合併邊框 */ }
4.在assets
下的js
資料夾中加入settings.js
export default {
base_url: 'http://127.0.0.1:8000'
}
5.前臺配置
安裝
cnpm install axios
cnpm install vue-cookies
cnpm install element-ui
cnpm install jquery
cnpm install bootstrap@3
在main.js
中配置
#axios配置
import axios from 'axios'
Vue.prototype.$axios = axios;
# vue-cookies配置
import cookies from 'vue-cookies'
Vue.prototype.$cookies = cookies;
# ElementUI的配置
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
# bootstrap配置
import 'bootstrap'
import 'bootstrap/dist/css/bootstrap.min.css'
jquery的配置,不太一樣(Vue中不推薦使用jquery,此條可忽略)
# 專案根路徑創一個 vue.config.js
const webpack = require("webpack");
module.exports = {
configureWebpack: {
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": "jquery",
"window.$": "jquery",
Popper: ["popper.js", "default"]
})
]
}
};
6.尾部元件
在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>
7.在components
內新建Banner.vue
,新增如下程式碼:
<template>
<div id="banner">
<el-carousel height="400px">
<el-carousel-item v-for="item in banner_list">
<!--<img src="../assets/img/banner1.png" alt="">-->
<router-link :to="item.link">
<img :src="item.img" :alt="item.name">
</router-link>
</el-carousel-item>
</el-carousel>
</div>
</template>
<script>
export default {
name: "Banner",
// data:function(){},
data() {
return {
banner_list: []
}
},
created() {
//當banner元件一建立,就向後臺發請求,拿回輪播圖資料
this.$axios.get(this.$settings.base_url + '/home/banner/').then(response => {
console.log(response.data)
this.banner_list = response.data
}).catch(error => {
})
},
}
</script>
<style scoped>
.el-carousel__item {
height: 400px;
min-width: 1200px;
}
.el-carousel__item img {
height: 400px;
/*margin-left: 20px;*/
/*margin-left: calc(50% - 1920px / 2);*/
}
</style>
8.在router
的index.js
中配置
const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/free-course',
name: 'FreeCourse',
component: FreeCourse
}, {
path: '/light-course',
name: 'LightCourse',
component: LightCourse
}, {
path: '/actual-course',
name: 'ActualCourse',
component: ActualCourse
},
]
三:DRF專案(luffyapi)輪播圖介面
1.apps
的home
的urls.py
from django.urls import path, re_path, include
from . import views
from rest_framework.routers import SimpleRouter
router = SimpleRouter()
router.register('banner', views.BannerView, 'banner')
urlpatterns = [
# path('banner/', views.BannerView.as_view()),
path('', include(router.urls)),
]
2.apps
的home
中的models.py
from django.db import models
# from luffyapi.utils.models import BaseModel
from utils.models import BaseModel
class Banner(BaseModel):
name = models.CharField(max_length=32, verbose_name='圖片名字')
img = models.ImageField(upload_to='banner', verbose_name='輪播圖', help_text='圖片尺寸必須是:3840*800', null=True)
link = models.CharField(max_length=32, verbose_name='跳轉連線')
info = models.TextField(verbose_name='圖片簡介')
# type=models.IntegerField(choices=)
def __str__(self):
return self.name
3.apps
的home
中建立serializer
,並新增程式碼
from rest_framework import serializers
from . import models
class BannerModelSerilaizer(serializers.ModelSerializer):
class Meta:
model = models.Banner
fields = ['name', 'link', 'img']
4.apps
的home
的views.py
from django.shortcuts import render
from rest_framework.views import APIView
from luffyapi.utils.response import APIResponse
from luffyapi.utils.logger import log
from rest_framework.mixins import ListModelMixin
from rest_framework.generics import GenericAPIView
from rest_framework.viewsets import GenericViewSet
from . import models
from . import serializer
# class BannerView(GenericAPIView,ListModelMixin): # 這個路由配置 path('banner/', views.BannerView.as_view()),
# 路由位置
'''
from rest_framework.routers import SimpleRouter
router=SimpleRouter()
router.register('banner',views.BannerView,'banner')
path('', include(router.urls)),
'''
from django.conf import settings
class BannerView(GenericViewSet, ListModelMixin):
# 無論有多少條待展示的資料,最多就展示3條
queryset = models.Banner.objects.filter(is_delete=False, is_show=True).order_by('display_order')[
:settings.BANNER_COUNTER]
serializer_class = serializer.BannerModelSerilaizer
5.在小luffyapi
建立utils
包,建立models.py
from django.db import models
# 後期課程表,輪播圖表,都會用到這些欄位
class BaseModel(models.Model):
create_time = models.DateTimeField(auto_now_add=True, verbose_name='建立時間')
update_time = models.DateTimeField(auto_now=True, verbose_name='最後更新時間')
is_delete = models.BooleanField(default=False, verbose_name='是否刪除')
is_show = models.BooleanField(default=True, verbose_name='是否展示')
display_order = models.IntegerField()
class Meta:
abstract=True # 一定不要忘了
6.在settings
資料夾中新建const.py
# 首頁輪播圖個數
BANNER_COUNTER=3
7.在dev.py
中新增程式碼
from .const import *