欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python Django项目实例一

程序员文章站 2022-07-14 22:39:49
...

Python Django项目实例

电子商务平台

项目功能

实现登录,注册,前端显示商品信息功能,加入购物车功能,以及购买功能,查看历史订单,强制登录等功能
Python Django项目实例一

后台处理登录方法

def login(request):#用户登录方法
    if request.method == 'GET':
        response = HttpResponseRedirect('landing')
        return response
    if request.method == 'POST':
        data = request.POST
        if data:
            # 获取到表单提交的值
            username =data.get('username')
            #print(username)
            password = data.get('password')
            #print(password)
            # 把表单中取到的值和数据库里做对比
            try:
                use = user.objects.get(u_name=username)
                if password==use.u_password:
                    #print("登录成功!")
                    request.session["is_login"]=True
                    request.session["u_name"]=use.u_name
                    request.session["u_password"]=use.u_password
                    request.session.set_expiry(0)
                    return HttpResponseRedirect('/loginededed')
                else:

                    print("密码和用户名不正确!")
                    return render_to_response('landing.html')
            except:
                print("未知错误!")
                return render_to_response('landing.html')
        else:
            print("表单提交为空!")
            return render_to_response('landing.html')

注册方法

def registeraction(request):
        if request.method == 'GET':
            return render(request, 'register.html')
        if request.method == 'POST':
            data = request.POST
            if data:
                # 获取到表单提交的值
                username = data.get('username')
                #print(username)
                password1 = data.get('password1')
                #print(password1)
                tellphone = data.get('tellphone')
                #print(tellphone)
                try:
                    user.objects.create(u_name=username,u_password=password1,u_tellphone=tellphone)
                    print("注册成功跳登录页面")
                    return render(request, 'landing.html')
                except:
                    print("注册失败跳注册页面")
                    return render(request, 'register.html')
        else:
            print("注册失败从新注册!")
            return render(request, 'register.html')

商品显示页面

Python Django项目实例一

后台代码

def grain_oil(request):
    if request.method == 'GET':
    #获取数据库商品类型为粮油米面商品
        commodit = commodity.objects.filter(com_type="粮油米面")
        # print(commodit)
        # print(commodit.count())
        # print(commodit.get(id='1').com_img)
        # commodit=serializers.serialize("json", commodit)     #将数据格式转化为json并序列化
        # print(commodit)
        return render(request, 'grain_oil.html', {'commodity': commodit})#传数据到前端

前端代码

前端代码商品显示特别重要

<script src="{% static 'landbs/jquery.min.js' %}"></script>
<script>//实现了商品数量加减,计算总价
        $(function () {
            for(k=1; k<=j;k++) {
                var quantity = "quantity" + k;
                var p = "p" + k;
                var add = "add" + k;
                var min = "min" + k;
                var total = "total" + k;
                var t = $("#" + quantity);
                var n = $("#" + p);
                var goshopcar="goshopcar"+k;
                var combuy="combuy"+k;
                $("#"+goshopcar).click(function () {
                    for(var i=1;i<=j+1;++i){document.getElementById("form"+i).action="/com_purchase";}
                });
                $("#"+combuy).click(function () {
                   for(var i=1;i<=j+1;++i){document.getElementById("form"+i).action="/com_buy";}
                });
                $("#" + add).click(function () {
                    var t = $(this).parent().find('input[class*=text]');
                    t.val(parseInt(t.val()) + 1);
                    $("#" + min).removeAttr("disabled");                 //当按加1时,解除$("#min")不可读状态
                    var n=$(this).parent().find('p[class*=p1]');
                    $(this).parent().find('p[class*=total]').html("总价:"+(parseInt(t.val()) * (parseInt(n.val()))).toFixed(2)+"元");
                });
                $("#" + min).click(function () {
                    var t = $(this).parent().find('input[class*=text]');
                    if (parseInt(t.val()) > 1) {                     //判断数量值大于1时才可以减少
                        t.val(parseInt(t.val()) - 1)
                    } else {
                        $("#" + min).attr("disabled", "disabled")        //当$("#min")为1时,$("#min")不可读状态
                    }
                    var n=$(this).parent().find('p[class*=p1]');
                     $(this).parent().find('p[class*=total]').html("总价:"+(parseInt(t.val()) * parseInt(n.val())).toFixed(2)+"元");
                });
                $("#" + total).html("总价:"+(parseInt(t.val()) * (parseInt(n.val()))).toFixed(2)+"元");
            }

        })
</script>
</head>
<body>
<script type="text/javascript">//动态显示每个商品
       var df = document.createDocumentFragment();
       j=0;
       var num=document.createElement("div");
           num.id="all";
           num.style.width="730px";
           num.style.height="800px";
           num.style.backgroundColor="antiquewhite";
       {% for i in commodity %}{//动态为数据库中每一商品创建一个div
           j++;

           form1=document.createElement("form");//创建form
           form1.id="form"+j;
           form1.className="form";
           form1.method="POST";
           var num1=document.createElement("div");
           num1.id="divv";
           num1.style.width="180px";
           num1.style.height="350px";
           num1.style.backgroundColor="aliceblue";
           num1.style.borderWidth="1px";
           num1.style.borderStyle="solid";
           num1.style.borderColor="cadetblue";
           num1.style.cssFloat="left";
           num.appendChild(num1);

           var img=document.createElement("img");
           img.src="/static/media/{{ i.com_img }}";
           img.alt="图片加载失败!!";
           var a=document.createElement("a");
           a.href="#/";
           a.appendChild(img);
           num1.appendChild(a);
           var num2=document.createElement("div");
           num2.id="div"+j;

           var p1=document.createElement("p");
           p1.id="p"+j;
           p1.className="p1";
           p1.value="{{ i.com_outprice }}";
           p1.innerHTML="单价:{{ i.com_outprice }}";

           var input3=document.createElement("input");
           input3.type="button";
           input3.id="min"+j;
           input3.className="min";
           input3.value="--";
           input3.float="left";

           var p2=document.createElement("p");
           p2.innerHTML="总价:";
           p2.className="total";
           p2.id="total"+j;

           var input6=document.createElement("input");
           input6.name="com_name";
           input6.value="{{ i.com_name }}";
           input6.type="hidden";
           var input7=document.createElement("input");
           input7.name="com_outprice";
           input7.value="{{ i.com_outprice }}";
           input7.type="hidden";
           num2.appendChild(input6);
           num2.appendChild(input7);

           var input4=document.createElement("input");
           input4.id="quantity"+j;
           input4.className="text";
           input4.name="num";
           input4.type="text";
           input4.value="1";
           input4.style.width="120px";

           var input5=document.createElement("input");

           input5.id="add"+j;
           input5.className="add";
           input5.type="button";
           input5.value="+";

           num2.appendChild(p1);
           num2.appendChild(input3);
           num2.appendChild(input4);
           num2.appendChild(input5);
           num2.appendChild(p2);

           var num3=document.createElement("div");
           var input1=document.createElement("input");
           input1.id="goshopcar"+j;
           input1.className="goshopcar";
           //input1.οnclick= function(){//如果点加入购物车,则submit提交方法为com——purchase也就是将form的action为com_purchase
             //document.getElementById("form").action="/com_purchase";
           //};
           input1.type="submit";
           input1.name="goshpcar";
           input1.style.width="88px";
           input1.value="加入购物车";

           var input2=document.createElement("input");
           input2.type="submit";
           input2.id="combuy"+j;
           //input2.οnclick=function(){//点购买,则submit提交方法为com——buy也就是将form的action为com_buy
               //document.getElementById("form").action="/com_buy";
           //};
           input2.className="combuy";
           input2.name="combuy";
           input2.style.width="88px";
           input2.value="购买";

           num3.appendChild(input1);
           num3.appendChild(input2);

           form1.appendChild(num2);//将num2添加到form
           form1.appendChild(num3);//将num3添加到form
           num1.appendChild(form1);//将form添加到num1块下
           df.appendChild(num);
       }
          {% endfor %}
    document.body.appendChild(df);
   </script>

商品加购物车,并判断用户是否已经登录

若用户已经登录,也就是有session,则点击商品加购物车能讲商品加入到某人的购物车下,若未登录,则强制到登录页面让用户登录

后台代码

def com_purchase(request):  # 商品加购物车页面
    if request.method == "POST":
        session = request.session.get('u_name')
        if session:
            print(session)
            useshopcarr = usershopcar.objects.filter(u_name=session).first()
            if useshopcarr is None:
                usershopcar.objects.create(u_name=session)
                useshopcarr=usershopcar.objects.get(u_name=session)
            data = request.POST
            com_name = data.get('com_name')
            com_num = data.get('num')
            print(com_num)
            #com_outprice = data.get('com_outprice')
            if useshopcarr.com1 =="" or useshopcarr.com1==com_name:
                #commodit1= commodity.objects.get(com_name=useshopcarr.com1)
                print(usershopcar.com1)
                if useshopcarr.com1 == com_name:
                    useshopcarr.num1=int(eval(useshopcarr.num1))+int(eval(com_num))
                else:
                    useshopcarr.num1=eval(com_num)
                    useshopcarr.com1 = com_name
                useshopcarr.save()
                print('进入了1号商品')
                return redirect('/shop_car')
            else:
                if useshopcarr.com2=="" or useshopcarr.com2==com_name:
                    print('进入了2号商品')
                    #commodit2 = commodity.objects.get(com_name=useshopcarr.com2)
                    if useshopcarr.com2==com_name:
                        useshopcarr.num2 = int(eval(com_num))+int(eval(useshopcarr.num2))
                    else:
                        useshopcarr.num2=eval(com_num)
                    useshopcarr.com2 = com_name
                    useshopcarr.save()
                    return redirect('/shop_car')
                else:
                    if useshopcarr.com3=="" or useshopcarr.com3==com_name:
                        print('进入了3号商品')
                        #commodit3 = commodity.objects.get(com_name=useshopcarr.com3)
                        if useshopcarr.com3 == com_name:
                            useshopcarr.num3 = int(eval(com_num))+int(eval(useshopcarr.num3))
                        else:
                            useshopcarr.num3 = eval(com_num)
                        useshopcarr.com3 = com_name
                        useshopcarr.save()
                        return redirect('/shop_car')
                    else:
                        if useshopcarr.com4=="" or useshopcarr.com4==com_name:
                            print('进入了4号商品')
                            #commodit4 = commodity.objects.get(com_name=useshopcarr.com4)
                            if useshopcarr.com4 == com_name:
                                useshopcarr.num4 = int(eval(com_num))+int(eval(useshopcarr.num4))
                            else:
                                useshopcarr.num4 = eval(com_num)
                            useshopcarr.com4 = com_name
                            useshopcarr.save()
                            return redirect('/shop_car')
                        else:
                            if useshopcarr.com5=="" or useshopcarr.com5==com_name:
                                print('进入了5号商品')
                                #commodit5 = commodity.objects.get(com_name=useshopcarr.com5)
                                if useshopcarr.com5 == com_name:
                                    useshopcarr.num5 = int(eval(com_num))+int(eval(useshopcarr.num5))
                                else:
                                    useshopcarr.num5 = eval(com_num)
                                useshopcarr.com5 = com_name
                                useshopcarr.save()
                                return redirect('/shop_car')
                            else:
                                if useshopcarr.com6=="" or useshopcarr.com6==com_name:
                                    print('进入了6号商品')
                                    #commodit6 = commodity.objects.get(com_name=useshopcarr.com6)
                                    if useshopcarr.com6 == com_name:
                                        useshopcarr.num6 = int(eval(com_num))+int(eval(useshopcarr.num6))
                                    else:
                                        useshopcarr.num6 = eval(com_num)
                                    useshopcarr.com6 = com_name
                                    useshopcarr.save()
                                    return redirect('/shop_car')
                                else:
                                    if useshopcarr.com7=="" or useshopcarr.com7==com_name:
                                        print('进入了7号商品')
                                        #commodit7 = commodity.objects.get(com_name=useshopcarr.com7)
                                        if useshopcarr.com7 == com_name:
                                            useshopcarr.num7 = int(eval(com_num))+int(eval(useshopcarr.num7))
                                        else:
                                            useshopcarr.num7 = eval(com_num)
                                        useshopcarr.com7 = com_name
                                        useshopcarr.save()
                                        return redirect('/shop_car')
                                    else:
                                        print('购物车只能放7个商品哦')
                                        return redirect('/shop_car')
        else:
            return redirect('/day_logins')
    else:
        return redirect('/day_logins')

上面的是将商品加入购物车,点击那个按键的后台代码,现在显示购物车页面,以及附上相关代码Python Django项目实例一
后台代码

def shop_car(request):#去购物车页面
    if request.method=="GET":
        session=request.session.get('u_name')
        if session:
            use = usershopcar.objects.filter(u_name=session).first()
            if use==None:
                usershopcar.objects.create(u_name=session)
                use = usershopcar.objects.get(u_name=session)
            if use.com1=="" and use.com2=="" and use.com3=="" and use.com4=="" and use.com5=="" and use.com6=="" and use.com7=="" :
                return render(request,'shop_car.html')
            else:
                commodit1 = commodity.objects.filter(com_name=use.com1)
                commodit2 = commodity.objects.filter(com_name=use.com2)
                commodit3 = commodity.objects.filter(com_name=use.com3)
                commodit4 = commodity.objects.filter(com_name=use.com4)
                commodit5 = commodity.objects.filter(com_name=use.com5)
                commodit6 = commodity.objects.filter(com_name=use.com6)
                commodit7 = commodity.objects.filter(com_name=use.com7)
                commodit=chain(commodit1 ,commodit2 ,commodit3 ,commodit4 ,commodit5 ,commodit6,commodit7)
                #print(type(use.num1))
                if use.num1:
                    num=[use.num1]
                    if use.num2:
                        num=[use.num1,use.num2]
                        if use.num3:
                            num=[use.num1,use.num2,use.num3]
                            if use.num4:
                                num=[use.num1,use.num2,use.num3,use.num4]
                                if use.num5:
                                    num = [use.num1, use.num2, use.num3, use.num4,use.num5]
                                    if use.num6:
                                        num = [use.num1, use.num2, use.num3, use.num4, use.num5,use.num6]
                                        if use.num7:
                                            num = [use.num1, use.num2, use.num3, use.num4, use.num5, use.num6,use.num7]
                else:
                    num=[]
                return render(request,"shop_car.html",{"commodity":commodit,'list':json.dumps(num)})
        else:
            return redirect('/day_logins')

前端代码显示购物车实现,这个页面特别难,适合有jsp能力的人看,代码太长,实现了全选,计算总价之类的,功能强大,谨慎观看

<html lang="en">
<head>
    {% load static %}
    <meta charset="UTF-8">
    <title>玛卡巴卡makabaka</title>
    <script src="{% static 'landbs/jquery.min.js' %}"></script>
        <script type="text/javascript">
        if(window.top !== window.self){ //若自身窗口不等于顶层窗口
            window.top.location.href = window.self.location.href; //顶层窗口跳转到自身窗口
         }
    </script>
    <style type="text/css">
        .top{
            width:990px;
            height:50px;
            background-image:url("{% static 'beijing.jpg' %}");
            margin: 1px auto;
        }
        .banner {
            width: 990px;
            height: 65px;
            background-color: #ccc;
            margin: 1px auto;
        }
        .content{
            width:990px;
            margin: 2px auto;
            overflow: hidden;
        }
        .management{
            width:990px;
            height:60px;
            margin: auto;
            background-color: aliceblue;
        }
        .buy{
            width:990px;
            height:60px;
            margin: auto;
            background-color: aliceblue;
        }
        .management table{
            position: relative;
            border: none;
            border-radius: 2px;
            display: flex;
            list-style: none;
            padding: 0px;
        }
        .botton{
            width:990px;
            height:90px;
            background-color: #ccc;
            margin: 0 auto;
        }
        .globalink{
            width:990px;
            height:55px;
            background-color: #ccc;
            margin: 0px auto;
        }
        #globalink .nav {
            position: relative;
            border: none;
            border-radius: 3px;
            display: flex;
            list-style: none;
            background: darkseagreen;
            padding: 8px;
        }#globalink .nav li {
            margin: 0px;
            }
        #globalink .nav li a {
            position: relative;
            padding: 0.6em 2em;
            font-size: 15px;
            margin-right: 10px;
            border: none;
            color: #333;
            display: inline-block;
            text-decoration: none;
            z-index: 3;
            }
        a:link,a:visited{
            color:blueviolet;
            text-decoration: aliceblue;
            margin-right: 18px;
        }
        a:hover{
            color:olivedrab;
            text-decoration: underline;
        }
        a:active{
            color: blue   ;
        }
    </style>
</head>
<body>
<div id="top" class="top">
    <table cellspacing="10" align="right">
        <tr>
            <td><a href="/myindent" target="_self">查看我的订单</a></td><!--链接到登录页面-->
            <td><a href="/logout" target="_self">注销</a></td><!--链接到注册页面-->
        </tr>
    </table>
</div><!--头部带登录注册-->
<div id="banner"  class="banner"><img src="{% static 'makabaka2.jpg' %}"></div><!--首部图片区-->
<!--<div id="globalink" class="globalink">导航链接</div>--><!--导航栏-->
<div id="globalink" class="globalink">
    <ul class="nav">
        <li><a href="#/" target="_self">网站首页</a></li>
        <li><a href="#/" target="right">水果生鲜</a></li>
        <li><a href="/daily_necessities" target="right">日用百货</a></li>
        <li><a href="/grain_oil" target="right">粮油米面</a></li>
        <li><a href="#/" target="right">洗护用品</a></li>
        <li><a href="/milk_drink" target="right">牛奶饮料</a> </li>
        <li><a href="#/" target="right">今日特价 </a> </li>
    </ul>
</div>
<script>
window.onload = function () {
        j=0;
        var shuliang = {{ list|safe }};
        var num=document.getElementById("content");
        {% for i in commodity %}{//动态为用户购物车中每一商品创建一个div
        j++;
        //form1 = document.createElement("form");//创建form
        //form1.id = "form" + j;
        //form1.className = "form";
        //form1.method = "POST";
        //form1.action="/com_buy";
        var num1 = document.createElement("div");
        num1.id = "divv";
        num1.style.width = "980px";
        num1.style.height = "180px";
        num1.style.backgroundColor = "aliceblue";
        num1.style.borderWidth = "1px";
        num1.style.borderStyle = "solid";
        num1.style.borderColor = "cadetblue";
        num1.style.cssFloat = "left";

        var chose=document.createElement("input");
        chose.type="checkbox";
        chose.className="checkbox";
        chose.id="checkbox"+j;
        chose.value="{{ i.com_name }}";
        chose.style.width="20px";
        chose.style.height="180px";
        chose.style.cssFloat="left";
        num1.appendChild(chose);
        var img = document.createElement("img");
        img.src = "/static/media/{{ i.com_img }}";
        img.alt = "图片加载失败!!";
        img.style.width="180px";
        img.style.height="180px";
        img.style.cssFloat="left";
        var a = document.createElement("a");
        a.href = "#/";
        a.appendChild(img);
        num1.appendChild(a);
        var num2 = document.createElement("div");
        num2.style.cssFloat="left";
        num2.id = "div2" + j;

        var pn=document.createElement("p");
        pn.id="pn"+j;
        pn.className="pn";
        pn.innerHTML="{{ i.com_name }}";
        pn.style.width="150px";
        pn.style.height="150px";
        pn.style.cssFloat="left";
        pn.style.marginLeft="60px";
        pn.style.marginright="20px";

        var p1 = document.createElement("p");
        p1.id = "p" + j;
        p1.className = "p1";
        p1.value = "{{ i.com_outprice }}";
        p1.innerHTML = "单价:{{ i.com_outprice }}";
        p1.style.width="100px";
        p1.style.height="150px";
        p1.style.cssFloat="left";
        p1.style.marginLeft="20px";

        var input3 = document.createElement("input");
        input3.type = "button";
        input3.id = "min" + j;
        input3.className = "min";
        input3.value = "--";
        input3.float = "left";
        input3.style.marginLeft="60px";

        var input6 = document.createElement("input");
        input6.name = "com_name";
        input6.className="comname";
        input6.value = "{{ i.com_name }}";
        input6.type = "hidden";

        var input8 = document.createElement("input");
        input8.name = "com_imgadd";
        input8.className="comimgadd";
        input8.value = "{{ i.com_img }}";
        input8.type = "hidden";

        var input7 = document.createElement("input");
        input7.name = "com_outprice";
        input7.className="comoutprice";
        input7.value = "{{ i.com_outprice }}";
        input7.type = "hidden";
        num2.appendChild(input6);
        num2.appendChild(input7);
        num2.appendChild(input8);

        var input4 = document.createElement("input");
        input4.id = "quantity" + j;
        input4.className = "text";
        input4.name = "num";
        input4.type = "text";
        input4.value =shuliang[j-1];
        input4.style.width = "80px";
        input4.style.marginLeft="2px";

        var input5 = document.createElement("input");

        input5.id = "add" + j;
        input5.className = "add";
        input5.type = "button";
        input5.value = "+";
        input5.style.marginLeft="2px";

        var p2 = document.createElement("p");
        p2.innerHTML = "总价:";
        p2.className = "total";
        p2.id = "total" + j;
        p2.style.width="100px";
        p2.style.cssFloat="right";
        p2.style.marginLeft="60px";

        num2.appendChild(pn);
        num2.appendChild(p1);
        num2.appendChild(input3);
        num2.appendChild(input4);
        num2.appendChild(input5);
        num2.appendChild(p2);

        //form1.appendChild(num2);//将num2添加到form
        //num1.appendChild(form1);
        num1.appendChild(num2);
        num.appendChild(num1);
    }
    {% endfor %}
$(function () {
    var x=0;
            for(k=1; k<=7;k++) {
                var chockboxid="checkbox"+k;
                var quantity = "quantity" + k;
                var p = "p" + k;
                var add = "add" + k;
                var min = "min" + k;
                var total = "total" + k;
                var t = $("#" + quantity);
                var n = $("#" + p);
                $("#"+chockboxid).click(function () {
                    var t = $(this).parent().find('input[class*=text]');
                        var n = $(this).parent().find('p[class*=p1]');
                    if(this.checked) {
                        x += (parseInt(t.val()) * parseInt(n.val()));
                        $("#pz").html("总计:" + parseInt(x).toFixed(2) + "元");
                    }
                    else {
                        x -= (parseInt(t.val()) * parseInt(n.val()));
                        $("#pz").html("总计:" + parseInt(x).toFixed(2) + "元");
                    }
                });
                $("#" + add).click(function () {
                    var t = $(this).parent().find('input[class*=text]');
                    t.val(parseInt(t.val()) + 1);
                    $("#" + min).removeAttr("disabled");                 //当按加1时,解除$("#min")不可读状态
                    var n=$(this).parent().find('p[class*=p1]');
                    $(this).parent().find('p[class*=total]').html("总价:"+(parseInt(t.val()) * (parseInt(n.val()))).toFixed(2)+"元");
                });
                $("#" + min).click(function () {
                    var t = $(this).parent().find('input[class*=text]');
                    if (parseInt(t.val()) > 1) {                     //判断数量值大于1时才可以减少
                        t.val(parseInt(t.val()) - 1)
                    } else {
                        $("#" + min).attr("disabled", "disabled")        //当$("#min")为1时,$("#min")不可读状态
                    }
                    var n=$(this).parent().find('p[class*=p1]');
                     $(this).parent().find('p[class*=total]').html("总价:"+(parseInt(t.val()) * parseInt(n.val())).toFixed(2)+"元");
                });
                $("#" + total).html("总价:"+(parseInt(t.val()) * (parseInt(n.val()))).toFixed(2)+"元");
            }
            //$("#pz").html("总计:"+parseInt(x).toFixed(2)+"元");
            $("#management").click(function () {//管理按钮按下时触发
                //var td2=document.getElementById("td9");
                var delete1=document.createElement("input");
                delete1.value="删除";
                delete1.style.width="80px";
                delete1.style.height="30px";
                delete1.style.fontSize="18px";
                delete1.style.backgroundColor="red";
                delete1.id="delete1";
                delete1.type="submit";
                $("#td9").html(delete1);
                $("#delete1").click(function () {
                document.getElementById("form1").action="/outcom_car";
                for(var i=0;i<=chosebox.length; i++){
                    if(chosebox[i].checked===false){
                      var lll = chosebox[i].id;
                      $("#" + lll).parent().find('input[class*=text]').attr("disabled",'disabled');
                      $("#" + lll).parent().find('input[class*=min]').attr("disabled",'disabled');
                      $("#" + lll).parent().find('input[class*=add]').attr("disabled",'disabled');
                      $("#" + lll).parent().find('input[class*=comoutprice]').attr("disabled",'disabled');
                      $("#" + lll).parent().find('input[class*=comname]').attr("disabled",'disabled');
                      $("#" + lll).parent().find('input[class*=comimgadd]').attr("disabled",'disabled');
                    }
                }
            });
            });
            var allchose=document.getElementById("allchose");
            var chosebox=document.getElementsByClassName("checkbox");
            allchose.onclick=function () {//全选按钮按下时触发
                x=0;
                for(var i=0;i<=chosebox.length; i++){//全选与全不选触发
                    chosebox[i].checked=this.checked;
                    if(this.checked===true) {
                        var lll = chosebox[i].id;
                        var t = $("#" + lll).parent().find('input[class*=text]');
                        var n = $("#" + lll).parent().find('p[class*=p1]');
                        x += (parseInt(t.val()) * parseInt(n.val()));
                        $("#pz").html("总计:" + parseInt(x).toFixed(2) + "元");
                    }
                    else {
                        x =0;
                        $("#pz").html("总计:" + parseInt(x).toFixed(2) + "元");
                    }
                }

            };
            for ( var i = 0; i < chosebox.length; i++){//当有商品不选时触发将全选设为空
		        chosebox[i].onclick = function (){
			        if ( !this.checked ){
				        allchose.checked = false;
			        }
		        };
	        }

            $("#account").click(function () {
                document.getElementById("form1").action="/com_buy";
                for(var i=0;i<=chosebox.length; i++){
                    if(chosebox[i].checked===false){
                      var lll = chosebox[i].id;
                      $("#" + lll).parent().find('input[class*=text]').attr("disabled",'disabled');
                      $("#" + lll).parent().find('input[class*=min]').attr("disabled",'disabled');
                      $("#" + lll).parent().find('input[class*=add]').attr("disabled",'disabled');
                      $("#" + lll).parent().find('input[class*=comoutprice]').attr("disabled",'disabled');
                      $("#" + lll).parent().find('input[class*=comname]').attr("disabled",'disabled');
                      $("#" + lll).parent().find('input[class*=comimgadd]').attr("disabled",'disabled');
                    }
                }
            });
        })
}
</script>
<div id="management"  class="management">
 <table>
        <tr>
            <td id="td1" class="td1"><input type="button" id="management" value="管理" style="width:80px;margin-left: 20px;font-size: 18px;"></td>
            <td id="td2" class="td2"><p style="font-size: 18px;margin-left: 160px;">商品名称</p></td>
            <td id="td3" class="td3"><p style="font-size: 18px;;margin-left: 100px;">商品单价</p></td>
            <td id="td4" class="td4"><p style="font-size: 18px;margin-left: 100px;">商品数量</p></td>
            <td id="td5" class="td5"><p style="font-size: 18px;margin-left: 100px;">商品总价</p></td>
        </tr>
</table>
</div>
<form method="post"  id="form1">
<div id="content" class="content">
</div>
<div id="buy"  class="buy">
 <table>
        <tr class="tr1" id="tr1">
            <td id="td7" class="td7"><input type="checkbox" id="allchose" style="width:20px;height: 30px; float: top" font-size="18px">全选</td>
            <td id="td8" class="td8"><p class="pz" id="pz"></p></td>
            <td id="td9" class="td9"><input type="submit" id="account" style="background-color: #ff5500; font-size: 18px; width: 80px" value="结算"></td>
        </tr>
</table>
</div>
</form>
<div id="botton" class="botton">底部部分</div><!--底部安全信息部分-->
</body>

接下来的用户订单,以及管理员相关功能,请查看我的Python Django开发实例二,并且我会在其中附上软件文件。
小白开发,还请高手多多指教,有什么问题欢迎指出。本文为原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。