• DJANGO-天天生鲜项目从0到1-012-订单-用户订单页面


    用户中心-订单页面展示

    编辑user/view.py文件,新增类UserOrderView

    class UserOrderView(LoginRequiredMixin, View):
        '''用户中心订单类'''
        template_name = 'user/user_center_order.html'
        context = {'type': ''}
    
        def get(self, request, page_num):
            '''显示用户订单'''
            self.context['type'] = 'order'
            user = request.user
            # 获取订单信息
            orders = OrderInfo.objects.filter(user=user).order_by('-create_time')
            # 遍历订单头
            for order in orders:
                order_goods_list = OrderGoods.objects.filter(order=order)
                # 动态给order增加属性
                order.order_goods_list = order_goods_list
                order.status_name = OrderInfo.ORDER_STATUS_DIC[order.order_status]
                order.method_name = OrderInfo.PAY_METHOD_DIC[str(order.pay_method)]
                # 遍历订单行
                for order_goods in order_goods_list:
                    # 动态给order_goods增加属性
                    order_goods.amount = order_goods.price * order_goods.count
    
            # 分页
            # 创建Paginator对象
            paginator = Paginator(orders, 3)
            total_page = paginator.num_pages
            # 校验参数页码
            if page_num > total_page:
                page_num = 1
            # 获取Page对象
            page = paginator.page(page_num)
            # 页码,页面上最多显示3页
            if total_page < 3:
                pages = range(1, total_page + 1)
            elif page_num < 2:
                pages = range(1, 4)
            elif page_num > total_page - 1:
                pages = range(total_page-2, total_page+1)
            else:
                pages = range(page_num-1, page_num + 2)
            # 组织上下文
            self.context['page'] = page
            self.context['pages'] = pages
            return render(request, self.template_name, self.context)

    1. 获取登录用户的订单头orders,循环遍历头,获取头下面对应的订单行order_goods,处理这种将头行结构传给模板时,可以先遍历头,然后查询出头下面的行,将返回的查询结果集当做一个属性,添加给头。这样只需要将头传递给模板文件即可

    # 遍历头
    for header in headers: # 查询行结果集 lines = Line.objects.filter(header=header) # 将行结果集当做属性添加给头 header.lines = lines
    # 将数据传递给模板文件
    return render(request, template_name, {'headers': headers})
    ...
    # 在模板文件中遍历头行
    {% for header in headers %}
    ....
      {% for line in header.lins %}
      ...
      {% end for %}
    {% end for %}

    2. 分页,在url中加入参数<int:pag_num>,展示具体第几页:

    • 导入分页模块:from django.core.paginator import Paginator
    • 根据订单头创建分页对象,每页显示三个订单:paginator = Paginator(orders, 3)
    • 校验url页码参数,若有误则将页码变量重置为第一页
    • 获取页码页面的page对象:page = paginator.page(page_num)
    • 设置页面下方页码列表的展示,如只展示5个页码
    # 页码,页面上最多显示5页
    # 显示的最小页码
    min_page = page_num - 2
    # 显示的最大页码
    max_page = page_num + 2
    # 若最小页码小于1,则设置最小页码为1
    # 并将小于的值加在最大页码上,保证最小页码到最大页码有5页
    diff = min_page - 1
    if diff < 0:
        min_page = 1
        max_page -= diff
    # 若最大页码大于总页数,则设置最大页码为总页数
    # 并将大于的值减在最小页码上,保证最小页码到最大页码有5页
    diff = max_page - total_page
    if diff > 0:
        max_page = total_page
        min_page -= diff
    # 设置显示的页码范围
    pages = range(min_page, max_page + 1)

    组织上下文,将page对象和页码列表传给模板:

    # 组织上下文
    self.context['page'] = page
    self.context['pages'] = pages
    return render(request, self.template_name, self.context)

    编辑url和模板文件

    {% extends 'base_user_center.html' %}
    {% load static %}
    {% block right %}
    <div class="right_content clearfix">
        <h3 class="common_title2">全部订单</h3>
        {% for order in page %}
        <ul class="order_list_th w978 clearfix">
            <li class="col01">{{ order.create_time }}</li>
            <li class="col02">{{ order.order_num }}</li>
            <li class="col02 stress">{{ order.status_name }}</li>
        </ul>
    
        <table class="order_list_table w980">
            <tbody>
                <tr>
                    <td width="55%">
                        {% for order_goods in order.order_goods_list %}
                        <ul class="order_goods_list clearfix">
                            <li class="col01"><a href="{% url 'goods:detail' order_goods.goods.id %}"><img src="{{ order_goods.goods.image.url }}"></a></li>
                            <li class="col02"><a href="{% url 'goods:detail' order_goods.goods.id %}">{{ order_goods.goods.name }}<em>{{ order_goods.goods.price }}元/{{ order_goods.goods.uom }}</em></a></li>
                            <li class="col03">{{ order_goods.count }}</li>
                            <li class="col04">{{ order_goods.amount }}</li>
                        </ul>
                        {% endfor %}
                    </td>
                    <td width="15%">{{ order.total_amount|add:order.transit_amount }}元(含运费)</td>
                    <td width="15%" class="status">{{ order.status_name }}</br>支付方式:{{ order.method_name }}</td>
                    {% csrf_token %}
                    <td width="15%"><a href="#" order_id='{{ order.id }}' order_status='{{ order.order_status }}' status_name='{{ order.status_name }}'pay_method='{{ order.pay_method }}' class="oper_btn">去付款</a></td>
                </tr>
            </tbody>
        </table>
        {% empty %}
        暂无订单
        {% endfor %}
    
        <div class="pagenation">
            {% if page.has_previous %}
            <a href="{% url 'user:order' page.previous_page_number %}"><上一页</a>
            {% endif %}
            {% for num in pages %}
            <a href="{% url 'user:order' num %}" {% if num == page.number %}class="active"{% endif %}>{{ num }}</a>
            {% endfor %}
            {% if page.has_next %}
            <a href="{% url 'user:order' page.next_page_number %}">下一页></a>
            {% endif %}
        </div>
    </div>
    {% endblock right %}
     
  • 相关阅读:
    简易表格练习
    CSS圆角样式
    力不从心
    学渣在努力~
    嫌疑人
    poj1308 Is it a tree?
    悲剧文本
    迷宫问题
    n皇后问题
    油田
  • 原文地址:https://www.cnblogs.com/gcxblogs/p/12895825.html
Copyright © 2020-2023  润新知