创建子应用
| |
| python manage.py startapp product |
| |
| |
| |
| |
| re_path(r'^', include(('product.urls', 'product'), namespace='product')), |
新建uitls.py工具类
| |
| |
| def daohang(category): |
| dict = {"category1": "", "category2": "", "category3": ""} |
| |
| if category.parent is None: |
| |
| dict["category1"] = category |
| elif category.subs.count() == 0: |
| |
| dict["category3"] = category |
| dict["category2"] = category.parent |
| dict["category1"] = category.parent.parent |
| |
| return dict |
创建视图函数
| |
| class ProductListView(View): |
| |
| def get(self, request, categoryId, pageNum): |
| try: |
| category = ProdctCategory.objects.get(id=categoryId) |
| except Exception: |
| return HttpResponseNotFound("分类不存在") |
| |
| |
| sort = request.GET.get("sort", "default") |
| |
| if sort == "price": |
| paixu = "price" |
| elif sort == "hot": |
| paixu = "-sale_count" |
| else: |
| sort = "default" |
| paixu = "id" |
| |
| |
| products = ProductSku.objects.filter(category=category, is_shop=True).order_by(paixu) |
| |
| pg = Paginator(products, 5) |
| |
| |
| try: |
| products_page = pg.page(pageNum) |
| except Exception: |
| return HttpResponseNotFound("分页不存在") |
| |
| |
| totalPages = pg.num_pages |
| |
| |
| data = { |
| "categories": product_category(), |
| "daohang": daohang(category), |
| "sort": sort, |
| "category": category, |
| "products_page": products_page, |
| "totalPages": totalPages, |
| "pageNum": pageNum, |
| |
| } |
| return render(request, "product/product_list.html", context=data) |
| |
创建路由
| urlpatterns = [ |
| re_path(r'productList/(?P<categoryId>\d+)/(?P<pageNum>\d+)/$', ProductListView.as_view(), name="productList"), |
| ] |
编写product.html
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> |
| <head> |
| <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> |
| <title>喵喵商城-商品列表</title> |
| <link rel="stylesheet" type="text/css" href="{{ static('css/jquery.pagination.css') }}"> |
| <link rel="stylesheet" type="text/css" href="{{ static('css/reset.css') }}"> |
| <link rel="stylesheet" type="text/css" href="{{ static('css/main.css') }}"> |
| <script type="text/javascript" src="{{ static('js/jquery-1.12.4.min.js') }}"></script> |
| <script type="text/javascript" src="{{ static('js/vue-2.5.16.js') }}"></script> |
| <script type="text/javascript" src="{{ static('js/axios-0.18.0.min.js') }}"></script> |
| <script type="text/javascript" src="{{ static('js/utils.js') }}"></script> |
| <script type="text/javascript" src="{{ static('js/jquery.pagination.min.js') }}"></script> |
| </head> |
| <body> |
| <div id="app"> |
| <div class="header_con"> |
| <div class="header" v-cloak> |
| <div class="welcome fl">欢迎来到喵喵商城</div> |
| <div class="fr"> |
| <div class="login_btn fl" v-show="flag1"> |
| 欢迎您:<em>{[ username ]}</em> |
| <span>|</span> |
| <a href="{{ url('users:logout') }}">退出</a> |
| </div> |
| <div class="login_btn fl" v-show="flag2"> |
| <a href="{{ url('users:login') }}">登录</a> |
| <span>|</span> |
| <a href="{{ url('users:zhuce') }}">注册</a> |
| </div> |
| <div class="user_link fl"> |
| <span>|</span> |
| <a href="{{ url('users:userinfo') }}">用户中心</a> |
| <span>|</span> |
| <a href="">我的购物车</a> |
| <span>|</span> |
| <a href="">我的订单</a> |
| </div> |
| </div> |
| </div> |
| </div> |
| <div class="search_bar clearfix"> |
| <a href="{{ url('shouye:index') }}" class="logo fl"><img src="{{ static('images/logo.png') }}"></a> |
| <div class="search_wrap fl"> |
| <form method="get" action="/search/" class="search_con"> |
| <input type="text" class="input_text fl" name="q" placeholder="搜索商品"> |
| <input type="submit" class="input_btn fr" name="" value="搜索"> |
| </form> |
| <ul class="search_suggest fl"> |
| <li><a href="#">满199减100</a></li> |
| <li><a href="#">家装建材</a></li> |
| <li><a href="#">潮流家电</a></li> |
| <li><a href="#">华为新品</a></li> |
| </ul> |
| </div> |
| <div class="guest_cart fr"> |
| <a href="" class="cart_name fl">我的购物车</a> |
| <div class="goods_count fl" id="show_count"></div> |
| <ul class="cart_goods_show"> |
| <li> |
| <img src="" alt="商品图片"> |
| <h4></h4> |
| <div></div> |
| </li> |
| </ul> |
| </div> |
| </div> |
| <div class="navbar_con"> |
| <div class="navbar"> |
| <div class="sub_menu_con fl"> |
| <h1 class="fl">商品分类</h1> |
| <ul class="sub_menu"> |
| {% for g in categories.values() %} |
| <li> |
| <div class="level1"> |
| {% for yiji in g.channels %} |
| <a href="{{ yiji.url }}">{{ yiji.name }}</a> |
| {% endfor %} |
| </div> |
| <div class="level2"> |
| <div class="list_group"> |
| {% for category2 in g.sub_categorys %} |
| <div class="group_name fl">{{ category2.name }} ></div> |
| <div class="group_detail fl"> |
| {% for category3 in category2.sub_categorys %} |
| <a href="/productList/{{ category3.id }}/1/">{{ category3.name }}</a> |
| {% endfor %} |
| </div> |
| {% endfor %} |
| </div> |
| </div> |
| </li> |
| {% endfor %} |
| </ul> |
| </div> |
| <ul class="navlist fl"> |
| <li><a href="">秒杀</a></li> |
| <li class="interval">|</li> |
| <li><a href="">拍卖</a></li> |
| <li class="interval">|</li> |
| <li><a href="">喵喵家电</a></li> |
| <li class="interval">|</li> |
| <li><a href="">喵喵超市</a></li> |
| <li class="interval">|</li> |
| <li><a href="">喵喵生鲜</a></li> |
| <li class="interval">|</li> |
| <li><a href="">喵喵国际</a></li> |
| </ul> |
| </div> |
| </div> |
| <div class="breadcrumb"> |
| <a href="javascript:;">{{ daohang.category1.name }}</a> |
| <span>></span> |
| <a href="javascript:;">{{ daohang.category2.name }}</a> |
| <span>></span> |
| <a href="javascript:;">{{ daohang.category3.name }}</a> |
| </div> |
| <div class="main_wrap clearfix"> |
| <div class="l_wrap fl clearfix"> |
| <div class="new_goods"> |
| <h3>热销排行</h3> |
| <ul> |
| <li v-for="pro in products"> |
| <a :href="pro.detailUrl"><img src="http://yhcsxtest.yonghui.cn//wimages/44788/main/44788_main_1657605282537.jpg"></a> |
| <h4><a :href="pro.detailUrl">{[ pro.name ]}</a></h4> |
| <div class="price">¥{[ pro.price ]}</div> |
| </li> |
| </ul> |
| </div> |
| </div> |
| <div class="r_wrap fr clearfix"> |
| <div class="sort_bar"> |
| <a href="{{ url('product:productList',args=(category.id,1)) }}?sort=default" |
| {% if sort == 'default' %}class="active" {% endif %}>默认</a> |
| <a href="{{ url('product:productList',args=(category.id,1)) }}?sort=price" |
| {% if sort == 'price' %}class="active" {% endif %}>价格</a> |
| <a href="{{ url('product:productList',args=(category.id,1)) }}?sort=hot" |
| {% if sort == 'hot' %}class="active" {% endif %}>销量</a> |
| </div> |
| <ul class="goods_type_list clearfix"> |
| {% for p in products_page %} |
| <li> |
| {# <a href=""><img src="{{ p.default_imgurl }}"></a>#} |
| <a href="{{ url('product:productDetail',args=(p.id,)) }}"><img |
| src="http://yhcsxtest.yonghui.cn//wimages/813860/main/813860_main_1657701335763.jpg"></a> |
| <h4><a href="{{ url('product:productDetail',args=(p.id,)) }}">{{ p.name }}</a></h4> |
| <div class="operate"> |
| <span class="price">¥{{ p.price }}</span> |
| <span class="unit">个</span> |
| <a href="#" class="add_goods" title="加入购物车"></a> |
| </div> |
| </li> |
| {% endfor %} |
| </ul> |
| {# 分页插件 #} |
| <div class="pagenation"> |
| <div id="pagination" class="page"></div> |
| </div> |
| </div> |
| </div> |
| <div class="footer"> |
| <div class="foot_link"> |
| <a href="#">关于我们</a> |
| <span>|</span> |
| <a href="#">联系我们</a> |
| <span>|</span> |
| <a href="#">招聘人才</a> |
| <span>|</span> |
| <a href="#">友情链接</a> |
| </div> |
| <p>CopyRight © 2018 北京*******有限公司 All Rights Reserved</p> |
| <p>电话:010-******* 京ICP备********号</p> |
| </div> |
| </div> |
| </body> |
| <script type="text/javascript"> |
| let categoryId = "{{ category.id }}" |
| </script> |
| |
| |
| <script type="text/javascript" src="{{ static('js/productlist.js') }}"></script> |
| <script type="text/javascript"> |
| $(function () { |
| $('#pagination').pagination({ |
| currentPage: {{ pageNum }}, |
| totalPage: {{ totalPages }}, |
| callback: function (current) { |
| location.href = '/productList/{{ category.id }}/' + current + '/?sort={{ sort }}' |
| } |
| }) |
| }) |
| |
| </script> |
| |
| </html> |
编写productlist.js
| var v = new Vue({ |
| el: "#app", |
| delimiters: ["{[", "]}"], |
| data: { |
| username: "", |
| |
| |
| flag1: false, |
| flag2: true, |
| |
| categoryId: categoryId, |
| products: "", |
| |
| }, |
| |
| mounted() { |
| |
| this.username = getCookie("username"); |
| |
| if (this.username != undefined) { |
| this.flag1 = true; |
| this.flag2 = false; |
| |
| } else { |
| this.flag1 = false; |
| this.flag2 = true; |
| } |
| |
| |
| this.get_remen_product(); |
| }, |
| methods: { |
| get_remen_product() { |
| if (this.categoryId) { |
| let path = "/hotProduct/" + this.categoryId + "/"; |
| axios.get(path).then(resp => { |
| this.products = resp.data.products; |
| |
| for (let i = 0; i < this.products.length; i++) { |
| this.products[i].detailUrl = '/productDetail/' + this.products[i].id + "/"; |
| |
| } |
| |
| |
| }).catch(err => { |
| console.log(err) |
| }) |
| } |
| } |
| } |
| |
| }) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通