Django+Vue开发生鲜电商平台之6.使用Vue实现商品类别和商品数据前台显示(下)

简介: 由之前的效果图和需求分析可知,首页全部商品分类需要展示一级、二级和三级分类,而在搜索结果页只展示一级和二级分类,分类有两个Vue组件,即Header中的全部商品分类和左侧的某以及类别对应的分类导航栏,也对应两个数据接口。

为了让数据在前端正常显示,还需要过滤类别数据、只显示一级类别,修改views.py如下:

class CategoryViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    '''
    List: 显示商品所有分类列表数据
    Retrieve: 获取单个商品分类详情
    '''
    queryset = GoodsCategory.objects.filter(category_type=1)
    serializer_class = CategorySerializer

此时再访问http://127.0.0.1:8080,显示:

image.jpeg

此时再访问,却访问不到商品分类,查看控制台提示很明显,同源策略禁止读取位于 http://127.0.0.1:8000/categorys/ 的远程资源。,即禁止跨域访问,当前端口是 8080,而数据接口端口是8000,因此被浏览器自动拒绝,一种方式是对服务器进行设置,还有一种是通过前端代理解决,这里采用第一种方式:

首先在虚拟环境中执行pip install django-cors-headers命令安装库,然后在后端settings.py中配置Cors:

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'apps.users.apps.UsersConfig',
    'goods.apps.GoodsConfig',
    'trade.apps.TradeConfig',
    'user_operation.apps.UserOperationConfig',
    'DjangoUeditor',
    'xadmin',
    'crispy_forms',
    'django.contrib.admin',
    'rest_framework',
    'django_filters',
    'corsheaders',
]
MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
# 跨域访问设置
CORS_ORIGIN_ALLOW_ALL = True

此时再访问,显示:

image.jpeg

此时不再报错,商品的各级分类也显示出来,但是可以看出来,全部分类右侧并未显示商品分类,这是因为一级分类的is_tab属性默认为False,改为True即可,可以在数据库中修改,也可以直接在后台管理系统修改。

head.vue中显示商品分类的逻辑也是通过循环实现,如下:

<template v-for="(item,index) in allMenuLabel">
    <li>
        <div v-if="item.is_tab">
            <router-link :to="'/app/home/list/'+item.id" >{{item.name}}</router-link>
        </div>
    </li>
</template>

后台修改商品分类is_tab属性并刷新页面如下:


image.jpeg


此时已经显示出商品分类。

三、Vue展示商品列表页数据和搜索

现在进一步实现点击某一个商品分类下面显示出商品详情,具体包括分类显示、价格筛选、分页和排序等功能。

通过项目可以看到,通过搜索和点击Tab页左侧显示的导航栏是不同的,其数据接口也不一样,head.vue如下:

<div class="head_search_hot">
  <span>热搜榜:</span>
  <router-link v-for="item in hotSearch" :to="'/app/home/search/'+item.keywords" :key="item.keywords">
    {{item.keywords}}
  </router-link>
</div>
// ...
<div class="main_nav_link" @mouseover="overAllmenu" @mouseout="outAllmenu">
    <a class="meunAll">全部商品分类
        <i class="iconfont">&#xe643;</i>
    </a>
    <div class="main_cata" id="J_mainCata" v-show="showAllmenu">
        <ul>
            <li class="first" v-for="(item,index) in allMenuLabel" @mouseover="overChildrenmenu(index)" @mouseout="outChildrenmenu(index)">
              <h3 style="background:url(../images/1449088788518670880.png) 20px center no-repeat;">
                <router-link :to="'/app/home/list/'+item.id">{{item.name}}</router-link> </h3>
                <div class="J_subCata" id="J_subCata" v-show="showChildrenMenu ===index"  style=" left: 215px; top: 0px;">
                    <div class="J_subView" >
                      <div v-for="list in item.sub_cat">
                        <dl>
                          <dt>
                            <router-link :to="'/app/home/list/'+list.id">{{list.name}}</router-link>
                          </dt>
                          <dd>
                            <router-link  v-for="childrenList in list.sub_cat" :key="childrenList.id" :to="'/app/home/list/'+childrenList.id">{{childrenList.name}}</router-link>
                          </dd>
                        </dl>
                        <div class="clear"></div>
                      </div>
                    </div>
                </div>
            </li>
        </ul>
    </div>
</div>

可以看到,跳转路由不同,搜索跳转到/app/home/search/,而点击导航栏跳转到/app/home/list/


再查看src/router/index.js如下:

{
  path: 'list/:id',
  name: 'list',
  component: list,
  meta: {
    title: '列表',
    need_log: false
  }
},
{
  path: 'search/:keyword',
  name: 'search',
  component: list,
  meta: {
    title: '搜索',
    need_log: false
  }
},

可以看到,两个路由绑定的组件是相同的,都是list,而在src/views/list/list.vue中:

getAllData () {
    console.log(this.$route.params)
    if(this.$route.params.id){
        this.top_category = this.$route.params.id;
        this.getMenu(this.top_category); // 获取左侧菜单列表
    }else{
        this.getMenu(null); // 获取左侧菜单列表
        this.pageType = 'search'
        this.searchWord = this.$route.params.keyword;
    }
    this.getCurLoc(); // 获取当前位置
    this.getListData(); //获取产品列表
    this.getPriceRange(); // 获取价格区间
},
getListData() {
    if(this.pageType=='search'){
      getGoods({
        search: this.searchWord, //搜索关键词
      }).then((response)=> {
        this.listData = response.data.results;
        this.proNum = response.data.count;
      }).catch(function (error) {
        console.log(error);
      });
    }else {
      getGoods({
        page: this.curPage, //当前页码
        top_category: this.top_category, //商品类型
        ordering: this.ordering, //排序类型
        pricemin: this.pricemin, //价格最低 默认为‘’ 即为不选价格区间
        pricemax: this.pricemax // 价格最高 默认为‘’
      }).then((response)=> {
        this.listData = response.data.results;
        this.proNum = response.data.count;
      }).catch(function (error) {
        console.log(error);
      });
    }
},
getMenu(id) {
    if(id != null){
      getCategory({
        id:this.$route.params.id
      }).then((response)=> {
        this.cateMenu = response.data.sub_cat;
        this.currentCategoryName = response.data.name
      }).catch(function (error) {
        console.log(error);
      });
    }else {
      getCategory({}).then((response)=> {
        this.cateMenu = response.data;
        this.isObject = false
      }).catch(function (error) {
        console.log(error);
      });
    }
},

可以看到,针对不同的参数有不同的请求方法和参数。


请求商品列表数据使用的是getListData()方法,调用了getGoods()方法,为了测试获取商品,将getGoodsAPI进行修改如下:

//获取商品列表
export const getGoods = params => { return axios.get(`${local_host}/goods/`, { params: params }) }

同时,向后端请求的参数有一个为top_category,即表示一级类别,请求该参数则返回这一类别下的所有类别,需要在后端定义一个过滤器,需要找到该一级分类下的所有二级分及其对应的商品,后端apps/goods/filters.py如下:

import django_filters
from django.db.models import Q
from .models import Goods
class GoodsFilter(django_filters.rest_framework.FilterSet):
    '''商品过滤类'''
    name = django_filters.CharFilter(field_name="name", lookup_expr='contains')
    pricemin = django_filters.NumberFilter(field_name="market_price", lookup_expr='gte')
    pricemax = django_filters.NumberFilter(field_name="market_price", lookup_expr='lte')
    top_category = django_filters.NumberFilter(method='top_category_filter')
    def top_category_filter(self, queryset, name, value):
        '''自定义过滤'''
        return queryset.filter(Q(category_id=value)|Q(category__parent_category_id=value)|Q(category__parent_category__parent_category_id=value))
    class Meta:
        model = Goods
        fields = ['name', 'pricemin', 'pricemax']

显示:

image.jpeg


此时,可以根据top_category进行筛选,再查看前端:


image.jpeg


可以看到,已经实现了价格筛选、排序、分页等功能。


从之前的代码还可以看到搜索功能的实现:

if(this.pageType=='search'){
  getGoods({
    search: this.searchWord, //搜索关键词
  }).then((response)=> {
    this.listData = response.data.results;
    this.proNum = response.data.count;
  }).catch(function (error) {
    console.log(error);
  });
}

再测试搜索功能如下:


2345_image_file_copy_28.jpg


显然,已经实现了搜索功能。

相关文章
|
4月前
|
开发框架 搜索推荐 数据可视化
Django框架适合开发哪种类型的Web应用程序?
Django 框架凭借其强大的功能、稳定性和可扩展性,几乎可以适应各种类型的 Web 应用程序开发需求。无论是简单的网站还是复杂的企业级系统,Django 都能提供可靠的支持,帮助开发者快速构建高质量的应用。同时,其活跃的社区和丰富的资源也为开发者在项目实施过程中提供了有力的保障。
172 62
|
4月前
|
设计模式 前端开发 数据库
Python Web开发:Django框架下的全栈开发实战
【10月更文挑战第27天】本文介绍了Django框架在Python Web开发中的应用,涵盖了Django与Flask等框架的比较、项目结构、模型、视图、模板和URL配置等内容,并展示了实际代码示例,帮助读者快速掌握Django全栈开发的核心技术。
301 45
|
4月前
|
SQL 监控 数据库
深入探索Django ORM:高效数据操作的秘诀与实践####
在当今的Web开发领域,提升数据访问层的效率是优化应用性能的关键。本文旨在通过剖析Django框架中的ORM(对象关系映射)机制,揭示其如何简化数据库交互,并探讨一系列高级技巧与策略,帮助开发者构建更高效、可维护的数据访问代码。我们不涉及安装步骤或基础概念,而是聚焦于实战经验分享,旨在为中高级开发者提供深度洞见。 ####
|
5月前
|
IDE 关系型数据库 MySQL
Django学习一:创建Django框架,介绍Django的项目结构和开发逻辑。创建应用,编写主包和应用中的helloworld
这篇文章是关于如何创建一个Django框架,介绍Django的项目结构和开发逻辑,并指导如何创建应用和编写“Hello, World!”程序的教程。
354 3
Django学习一:创建Django框架,介绍Django的项目结构和开发逻辑。创建应用,编写主包和应用中的helloworld
|
4月前
|
安全 数据库 开发者
Python Web开发:Django框架下的全栈开发实战
【10月更文挑战第26天】本文详细介绍了如何在Django框架下进行全栈开发,包括环境安装与配置、创建项目和应用、定义模型类、运行数据库迁移、创建视图和URL映射、编写模板以及启动开发服务器等步骤,并通过示例代码展示了具体实现过程。
127 2
|
5月前
|
JavaScript 前端开发 Python
django接收前端vue传输的formData图片数据
django接收前端vue传输的formData图片数据
91 4
|
5月前
|
数据库 数据安全/隐私保护 数据库管理
#765372#基于django和neo4j的通用数据展示系统
#765372#基于django和neo4j的通用数据展示系统
39 1
|
3月前
|
JavaScript
vue使用iconfont图标
vue使用iconfont图标
162 1
|
21天前
|
移动开发 JavaScript API
Vue Router 核心原理
Vue Router 是 Vue.js 的官方路由管理器,用于实现单页面应用(SPA)的路由功能。其核心原理包括路由配置、监听浏览器事件和组件渲染等。通过定义路径与组件的映射关系,Vue Router 将用户访问的路径与对应的组件关联,支持哈希和历史模式监听 URL 变化,确保页面导航时正确渲染组件。
|
25天前
|
监控 JavaScript 前端开发
ry-vue-flowable-xg:震撼来袭!这款基于 Vue 和 Flowable 的企业级工程项目管理项目,你绝不能错过
基于 Vue 和 Flowable 的企业级工程项目管理平台,免费开源且高度定制化。它覆盖投标管理、进度控制、财务核算等全流程需求,提供流程设计、部署、监控和任务管理等功能,适用于企业办公、生产制造、金融服务等多个场景,助力企业提升效率与竞争力。
83 12

热门文章

最新文章