zoukankan      html  css  js  c++  java
  • 6.获取片区轮播图

    获取片区轮播图

    1.在后端NewCenter部分:

    1.在apps/users/views.py中:

    from django.shortcuts import render,HttpResponse
    from rest_framework.views import APIView,Response
    from random import choice
    from .models import UserProfile,PianQu,LouMing
    import datetime,json,time,hashlib
    from .serializers import PianQuModelSerializer
    # Create your views here.
    
    
    #……
    
    class GetPianquListView(APIView):
        """获取片区列表"""
    
        def get(self, request):
            token = request.GET.get('roottoken')
            #print(token)
            if token:
                user = UserProfile.objects.filter(username='admin',token=token).first()
                if user:
                    p_list=PianQu.objects.all()
                    re=PianQuModelSerializer(p_list,many=True)
                    return Response(re.data)
                else:
                    result = {"status": "403", "data": {'msg': '请重新登录。'}}
            else:
                result = {"status": "403", "data": {'msg': '参数不足'}}
            return HttpResponse(json.dumps(result, ensure_ascii=False), content_type="application/json,charset=utf-8")

    2.在apps/users目录下新建序列化文件serializers.py:

    from rest_framework import serializers
    from .models import UserProfile,PianQu,LouMing,DanYuan,ZhuHu
    
    
    class UserProfileModelSerializer(serializers.ModelSerializer):
        class Meta:
            model = UserProfile
            fields=("id","username","name","minzu","sex","mobile","address","danwei","zhiwei","is_dangyuan","hujixz","huji","wenhua",
                    "hunyin","no","chusheng","is_teshu","teshu","beizhu","power","img","work")
    
    class PianQuModelSerializer(serializers.ModelSerializer):
        class Meta:
            model = PianQu
            fields="__all__"
    
    
    class LouMingModelSerializer(serializers.ModelSerializer):
        class Meta:
            model = LouMing
            fields="__all__"
    
    
    class DanYuanModelSerializer(serializers.ModelSerializer):
        class Meta:
            model = DanYuan
            fields="__all__"
    
    
    class ZhuHuModelSerializer(serializers.ModelSerializer):
        class Meta:
            model = ZhuHu
            fields="__all__"

    3.在apps/users/urls.py中:

    from django.urls import path
    from .views import RootLoginView,GetPianquListView
    
    urlpatterns = [
        path('rlogin/',RootLoginView.as_view()),#管理员登录
        path('getpqlist/',GetPianquListView.as_view()),#获取片区列表
    ]

    2.在newpc前端部分:

    1.在src/api/api.js中:

    import { get, post } from './request'
    const host='http://127.0.0.1:8000'
    const media_host='http://127.0.0.1:8000/media/'
    
    export {
        host,
        media_host
    }
    
    //登录
    export function tologin(params2) {
        return post(host+'/users/rlogin/', {pwd:params2});
    }
    
    //获取片区列表
    export function getpianqulist(params2) {
        return get(host+'/users/getpqlist/', {roottoken:params2});
    }

    2.在src/components/Index.vue中:

    <template>
      <div id="index">
        
          <div class="content">
                <el-carousel :interval="3000" type="card" height="600px">
                    <el-carousel-item v-for="(item,index) in data" :key="index" >
                        <img :src="item.banner" :alt="item.title" @click="ToLou(item.id)">
                    </el-carousel-item>
                </el-carousel>
          </div>
    
      </div>
    </template>
    <script>
    // 引入模块
    import storage from '@/storage.js';
    import { host,getpianqulist } from "@/api/api.js";
    export default {
      name: 'index',
      data () {
        return {
          msg:'首页',
          data:[
          ]
        }
      },
      methods:{
        //跳转到楼列表页
        ToLou(e){
              e=e+""
              this.$router.push({path:'/loulist.html?aid='+e})
          },
        //查看是否登录
        me_init(){
          if(storage.get('roottoken')){
            return true
          }else{
            this.$router.push({path:'/login.html'})
          }
        },
        //获取片区列表信息
        getPian(){
          console.log('获取片区列表')
          // ----向后端发送数据开始----
          getpianqulist(storage.get('roottoken')).then(res => {
              // console.log(res)
              if(res.status==403){
                storage.remove('roottoken')
                this.$router.push({path:'/login.html'})
              }else{
                for(var i=0;i<res.length;i++){
                  res[i]["banner"]=host+res[i]["banner"]
                }
                this.data=res
              }
          }).catch(error => {console.log(error);});
          // -----向后端发送数据结束-----
        }
      },
      
      mounted(){
        this.me_init()
        this.getPian()
      }
    }
    </script>
    <style scoped>
    .content{
        /*  80%;
        margin: 0 auto; */
        margin-top: 60px;
        margin-bottom: 60px;
    }
      .el-carousel__item h3 {
        color: #475669;
        font-size: 14px;
        opacity: 0.75;
        line-height: 200px;
        margin: 0;
      }
      
      .el-carousel__item:nth-child(2n) {
        background-color: #99a9bf;
      }
      
      .el-carousel__item:nth-child(2n+1) {
        background-color: #d3dce6;
      }
      img{
        /*设置图片宽度和浏览器宽度一致*/
        width: 100%;
        height: inherit;
      }
    </style>
  • 相关阅读:
    贪心法(三):POJ题库中的贪心法应用例题
    贪心法(四):HDU题库中的贪心法应用例题
    C语言程序设计100例之(41):快速幂运算
    C语言程序设计100例之(36):四方定理
    C语言程序设计100例之(40):最大公约数问题
    C语言程序设计100例之(38):涂国旗
    相关性搜索简介——常规技术与应用
    Launch X431 HD3卡车诊断适配器评论和反馈
    如何修复更新时卡住的 GODIAG GD801?
    Highend扫描仪比较:Autel vs Snapon vs. Launch vs Bosch
  • 原文地址:https://www.cnblogs.com/xuepangzi/p/13092525.html
Copyright © 2011-2022 走看看