• django-评论


    视图函数views.py

    # 订单评论
    class OrderCommentView(View):
        def get(self, request, order_id):
            # 获取用户信息
            user = request.user
            # 获取用户所有订单的信息
            try:
                order = OrderInfo.objects.get(user=user, order_id=order_id)
            except Exception as e:
                return redirect(reverse("user:order"))
            # 获取订单中的商品信息
            order_skus = OrderGoods.objects.filter(order_id=order.order_id)
            # 遍历订单中的商品信息
            for order_sku in order_skus:
                # 计算小计
                amount = order_sku.price * order_sku.count
                # 动态给order_skus增加小计amount 保存订单小计
                order_sku.amount = amount
    
            # 动态给order添加商品信息
            order.order_skus = order_skus
            # 动态给order添加支付信息
            order.status_name = OrderInfo.ORDER_STATUS[order.order_status]
            return render(request, 'order_comment.html', {'order':order})
    
        def post(self, request, order_id):
            """处理评论内容"""
            user = request.user
            # 校验数据
            if not order_id:
                return redirect(reverse('user:order'))
    
            try:
                order = OrderInfo.objects.get(order_id=order_id, user=user)
            except OrderInfo.DoesNotExist:
                return redirect(reverse("user:order"))
    
            # 获取评论条数
            total_count = request.POST.get("total_count")
            total_count = int(total_count)
    
            # 循环获取订单中商品的评论内容
            for i in range(1, total_count + 1):
                # 获取评论的商品的id
                sku_id = request.POST.get("sku_%d" % i) # sku_1 sku_2
                # 获取评论的商品的内容
                content = request.POST.get('content_%d' % i, '') # cotent_1 content_2 content_3
                try:
                    order_goods = OrderGoods.objects.get(order=order, sku_id=sku_id)
                except OrderGoods.DoesNotExist:
                    continue
    
                order_goods.comment = content
                order_goods.save()
    
            order.order_status = 5 # 已完成
            order.save()
    
            return redirect(reverse("user:order", kwargs={"page": 1}))

    模板order_comment.html中

    {% extends 'base_user_center.html' %}
    {% load staticfiles %}
    {% block title %}天天生鲜-用户中心{% endblock %}
    {% block page_title %}用户中心{% endblock page_title %}
    {% block right_content %}
            <div class="right_content clearfix">
                <h3 class="common_title2">订单评价</h3>
                    <ul class="order_list_th w978 clearfix">
                        <li class="col01">{{order.create_time}}</li>
                        <li class="col02">订单号:{{order.order_id}}</li>
                        <li class="col02 stress">{{order.status_name}}</li>
                    </ul>
                <form method="post">
                    {% csrf_token %}
                    {# 订单id #}
                    <input type="hidden" name="order_id" value="{{order.order_id}}">
                    {# 订单中有几个商品 #}
                    <input type="hidden" name="total_count" value="{{order.order_skus|length}}">
                    {% for order_sku in order.order_skus %}
                    <table class="order_list_table w980">
                        <tbody>
                            <tr>
                                <td width="80%">
                                    <ul class="order_goods_list clearfix">
                                        <li class="col01"><img src="{{ order_sku.sku.image.url }}"></li>
                                        <li class="col02">{{order_sku.sku.name}}<em>{{order_sku.price}}/{{order_sku.sku.unite}}</em></li>
                                        <li class="col03">{{order_sku.count}}</li>
                                    </ul>
                                </td>
                                <td width="20%">{{order_sku.amount}}元</td>
                            </tr>
                        </tbody>
                    </table>
                    <div class="site_con">
                        <input type="hidden" name="sku_{{forloop.counter}}" value="{{order_sku.sku.id}}">
                        <div class="form_group form_group2">
                            <label>评价内容:</label>
                            <textarea class="site_area" name="content_{{forloop.counter}}"></textarea>
                        </div>
                    </div>
                    {% endfor %}
                    <input type="submit" name="" value="提交" class="info_submit">
                </form>
            </div>
    {% endblock right_content %}

    详情页面detail.html中

    {% extends 'layout/base_list_detail.html' %}
    {% load staticfiles %}
    {% block title %}天天生鲜-商品详情{% endblock title %}
    
    {% block main_content %}
        <div class="breadcrumb">
            <a href="#">全部分类</a>
            <span>></span>
            <a href="#">{{ sku.type.name }}</a>
            <span>></span>
            <a href="#">商品详情</a>
        </div>
    
        <div class="goods_detail_con clearfix">
            <div class="goods_detail_pic fl"><img src="{{ sku.image.url }}"></div>
    
            <div class="goods_detail_list fr">
                <h3>{{ sku.name }}</h3>
                <p>{{ sku.desc }}</p>
                <div class="prize_bar">
                    <span class="show_pirze">¥<em>{{ sku.price }}</em></span>
                    <span class="show_unit">单  位:{{ sku.unite }}</span>
                </div>
                <div class="goods_num clearfix">
                    <div class="num_name fl">数 量:</div>
                    <div class="num_add fl">
                        <input type="text" class="num_show fl" value="1">
                        <a href="javascript:;" class="add fr">+</a>
                        <a href="javascript:;" class="minus fr">-</a>
                    </div>
                </div>
                <div>
                    <p>其他规格:</p>
                    <ul>
                        {% for sku in same_spu_skus %}
                            <li><a href="{% url 'goods:detail' sku.id %}">{{ sku.name }}</a></li>
                        {% endfor %}
                    </ul>
                </div>
    
                <div class="total">总价:<em>16.80元</em></div>
                <div class="operate_btn">
                    {% csrf_token %}
                    <a href="javascript:;" class="buy_btn">立即购买</a>
                    <a href="javascript:;" sku_id="{{ sku.id }}" class="add_cart" id="add_cart">加入购物车</a>
                </div>
            </div>
        </div>
    
        <div class="main_wrap clearfix">
            <div class="l_wrap fl clearfix">
                <div class="new_goods">
                    <h3>新品推荐</h3>
                    <ul>
                        {% for sku in new_skus %}
                        <li>
                            <a href="{% url 'goods:detail' sku.id %}"><img src="{{ sku.image.url }}"></a>
                            <h4><a href="{% url 'goods:detail' sku.id %}">{{ sku.name }}</a></h4>
                            <div class="prize">¥{{ sku.price }}</div>
                        </li>
                        {% endfor %}
                    </ul>
                </div>
            </div>
    
            <div class="r_wrap fr clearfix">
                <ul class="detail_tab clearfix">
                    <li id="tag_detail" class="active">商品介绍</li>
                    <li id="tag_comment">评论</li>
                </ul>
    
                <div class="tab_content" id="order_content">
                    <dl>
                        <dt>商品详情:</dt>
                        <dd>{{ sku.goods.detail|safe }}</dd>
                    </dl>
                </div>
    
                <div class="tab_content" id="order_comment" style="display: none">
                    <dl>
                        {% for order in sku_orders %}
                        <dt>评论时间:{{ order.update_time }}&nbsp;&nbsp;用户名:{{ order.order.user.username }}</dt>
                        <dd>评论内容:{{ order.comment }}</dd>
                        {% endfor %}
                    </dl>
                </div>
            </div>
    
    
        </div>
    {% endblock main_content %}
    {% block bottom %}
        <div class="add_jump"></div>
    {% endblock bottom %}
    {% block bottomfiles %}
        <script type="text/javascript" src="{% static 'js/jquery-1.12.4.min.js' %}"></script>
        <script type="text/javascript">
    
             $('#tag_detail').click(function () {
                $('#tag_comment').removeClass('active')
                $(this).addClass('active')
                $('#order_content').show()
                $('#order_comment').hide()
            })
    
            $('#tag_comment').click(function () {
                $('#tag_detail').removeClass('active')
                $(this).addClass('active')
                $('#order_content').hide()
                $('#order_comment').show()
            })
            update_goods_amount()
            // 计算商品的总价格
            function update_goods_amount() {
                // 获取商品的单价和数量
                price = $('.show_pirze').children('em').text()
                count = $('.num_show').val()
                // 计算商品的总价
                price = parseFloat(price)
                count = parseInt(count)
                amount = price*count
                // 设置商品的总价
                $('.total').children('em').text(amount.toFixed(2)+'')
            }
    
            // 增加商品的数量
            $('.add').click(function () {
                // 获取商品原有的数目
                count = $('.num_show').val()
                // 加1
                count = parseInt(count)+1
                // 重新设置商品的数目
                $('.num_show').val(count)
                // 更新商品的总价
                update_goods_amount()
            })
    
            // 减少商品的数量
            $('.minus').click(function () {
                // 获取商品原有的数目
                count = $('.num_show').val()
                // 减1
                count = parseInt(count)-1
                if (count <= 0){
                    count = 1
                }
                // 重新设置商品的数目
                $('.num_show').val(count)
                // 更新商品的总价
                update_goods_amount()
            })
    
            // 手动输入商品的数量
            $('.num_show').blur(function () {
                // 获取用户输入的数目
                count = $(this).val()
                // 校验count是否合法
                if (isNaN(count) || count.trim().length==0 || parseInt(count) <=0){
                    count = 1
                }
                // 重新设置商品的数目
                $(this).val(parseInt(count))
                // 更新商品的总价
                update_goods_amount()
            })
    
            // 获取add_cart div元素左上角的坐标
            var $add_x = $('#add_cart').offset().top;
            var $add_y = $('#add_cart').offset().left;
    
            // 获取show_count div元素左上角的坐标
            var $to_x = $('#show_count').offset().top;
            var $to_y = $('#show_count').offset().left;
    
    
            $('#add_cart').click(function(){
                // 获取商品id和商品数量
                sku_id = $(this).attr('sku_id') // attr prop
                count = $('.num_show').val()
                csrf = $('input[name="csrfmiddlewaretoken"]').val()
                // 组织参数
                params = {'sku_id':sku_id, 'count':count, 'csrfmiddlewaretoken':csrf}
                // 发起ajax post请求,访问/cart/add, 传递参数:sku_id count
                $.post('/cart/add', params, function (data) {
                    if (data.res == 5){
                        // 添加成功
                        $(".add_jump").css({'left':$add_y+80,'top':$add_x+10,'display':'block'})
                        $(".add_jump").stop().animate({
                            'left': $to_y+7,
                            'top': $to_x+7},
                            "fast", function() {
                                $(".add_jump").fadeOut('fast',function(){
                                    // 重新设置用户购物车中商品的条目数
                                    $('#show_count').html(data.total_count);
                                });
                        });
                    }
                    else{
                        // 添加失败
                        alert(data.errmsg)
                    }
                })
            })
        </script>
    {% endblock bottomfiles %}
  • 相关阅读:
    BootStrap 学习笔记一
    ROW_NUMBER() OVER的用法
    Angularjs学习笔记(五)----显示和格式化数据
    &&和||的妙用
    形象的讲解angular中的$q与promise(转)
    Angularjs学习笔记(四)----与后端服务器通信
    Angularjs学习笔记(一)
    Angularjs学习笔记(二)----模块
    Angularjs学习笔记(三)----依赖注入
    StringBuilder类型
  • 原文地址:https://www.cnblogs.com/yifengs/p/11693333.html
Copyright © 2020-2023  润新知