django项目开发之订单模块

1.订单模块

当购物车商品筛选完毕点击结算时,跳转到支付宝支付流程,其实就是接入到支付宝的支付接口,由支付宝来处理订单结算,我们直接获取支付结果。支付模块可以直接查看支付宝的开发文档。

1.1 表的关联

一个用户可以有多个订单
一个订单中可以有多个商品
一个商品可以在多个订单中
订单和商品是多对多的关系

1.2 建立模型

class Order(models.Model):
    o_user=models.ForeignKey(User)
    o_price=models.FloatField(default=0)
    o_time=models.DateTimeField(auto_now=True)
    o_status=models.IntegerField(default=ORDER_STATUS_NOT_PAY)

    class Meta:
        db_table='sxw_order'

class OrderGoods(models.Model):
    o_order=models.ForeignKey(Order)
    o_goods=models.ForeignKey(Goods)
    o_goods_num=models.IntegerField(default=1)

    class Meta:
        db_table='sxw_ordergoods'

执行数据迁移

1.3 点击结算按键时,生成订单

在views.py文件中写视图函数

def make_order(request):
    carts=Cart.objects.filter(c_user=request.user).filter(c_is_select=True)
    order=Order()
    order.o_user=request.user
    order.o_price=get_total_price()
    order.save()
    for cart_obj in carts:
        ordergoods=OrderGoods()
        ordergoods.o_order=order
        ordergoods.o_goods_num=cart_obj.c_goods_num
        ordergoods.o_goods=cart_obj.c_goods
        ordergoods.save()
        cart_obj.delete()
    data={
     
        'status':200,
        'msg':'ok',
        'order_id':order.id
    }
    return JsonResponse(data=data)

配置路由

url(r'^makeorder/',views.make_order,name='make_order'),

cart.js

$("#make_order").click(function(){
     
        var select_list=[];
        var unselect_list=[];
        $(".confirm").each(function(){
     
            var $confirm=$(this);
            var cartid =$confirm.parents("li").attr("cartid");
            if($confirm.find("span").find("span").html().trim()){
     
                select_list.push(cartid);
            }else{
     
                unselect_list.push(cartid);
            }
        })
        if(select_list.length === 0){
     
            return
        }
        $getJSON("/sxw/makeorder/",function(data){
     
            console.log(data);
            if(data['status'] === 200){
     
                window.open('/sxw/orderdetail/?orderid=' + data['order_id'],target='_self');
            }
        })
    })

1.4 订单详情

订单生成后,需要一个页面来显示订单详情。先创建个订单模板页

base_order.html

{
     % extends 'base.html' %}
{
     % load static %}

{
     % block ext_css %}
    {
     {
      block.super }}
    <link rel="stylesheet" href="{% static 'main/css/order.css' %}">
{
     % endblock %}

{
     % block header %}
    <header></header>
{
     % endblock %}

order_detail.html

{
     % extends 'base_order.html' %}
{
     % load static %}

{
     % block ext_css %}
    {
     {
      block.super }}
    <link rel="stylesheet" href="{% static 'main/css/order_detail.css' %}">
{
     % endblock %}

{
     % block ext_js %}
    {
     {
      block.super }}
    <script type="text/javascript" src="{% static 'main/js/order_detail.js' %}"></script>
{
     % endblock %}

{
     % block content %}
<div id="order_detail" class="container">
    <h6>订单编号:{
     {
     order.id}}</h6>
    <ul>
        {
     % for ordergoods in order.ordergoods_set.all %}
        <li>
            <img src="{
     {ordergoods.o_goods.productimg }}" alt="{
     {ordergoods.o_goods.productlongname}}">
        </li>
        {
     % endfor%}
    </ul>
    <ul>
        {
     % for ordergoods in order.ordergoods_set.all %}
        <li class="menulist">

            <a href="#">
                <img src="{
     { ordergoods.o_goods.productimg }}" alt="{
     {ordergoods.o_goods.productlongname}}">
                <p>{
     {
     ordergoods.o_goods.productlongname }}</p>
                <p class="presentPrice">{
     {
     ordergoods.o_goods.price}}</p>
            </a>
            <section>

                <span>{
     {
     ordergoods.o_goods_num}}</span>

            </section>

        </li>
        {
     % endfor%}
    </ul>
    <h6  class="total_price">总价:<span id="total_price">{
     {
     order.o_price}}</span>></h6>
    <button orderid="{
     {order.id}}" id="alipay" class="btn btn-success btn-block">支付</button>
</div>
{
     % endblock %}

订单详情的视图函数

def order_detail(request):
    order_id=request.GET.get('orderid')
    order=Order.objects.get(pk=order_id)
    data={
     
        'title':'订单详情',
        'order':order,
    }
    return render(request,'order_detail.html',context=data)

配置路由

url(r'^orderdetail/',views.order_detail,name='order_detail'),

1.5 未支付订单

首先在views.py写未支付订单的视图函数

def order_list_not_pay(request):
    orders=Order.objects.filter(o_user=request.user).filter(o_status=ORDER_STATUS_NOT_PAY)
    data={
     
        'title':'订单列表',
        'orders':orders,
    }
    return render(request,'order_list_not_pay.html',context=data)

配置路由

url(r'^orderlistnotpay/',views.order_list_not_pay,name='order_list_not_pay'),

order_list_not_pay.html

{
     % extends 'base_order.html' %}
{
     % load static %}

{
     % block ext_css %}
{
     {
      block.super }}
<link rel="stylesheet" href="{% static 'main/css/order_list_not_pay.css' %}">
{
     % endblock %}

{
     % block ext_js %}
{
     {
      block.super }}
<script type="text/javascript" src="{% static 'main/js/order_list_not_pay.js' %}"></script>
{
     % endblock %}

{
     % block content %}
<div id="order_list_not_pay" class="container">
    <ol>
        {
     % for order in orders %}
        <li>
            <ul class="order" orderid="{
     {order.id}}">
                <h6>订单编号:{
     {
     order.id}}</h6>
                {
     % for ordergoods in order.ordergoods_set.all %}
                <li class="menulist">

                    <a href="#">
                        <img src="{
     { ordergoods.o_goods.productimg }}" alt="{
     {ordergoods.o_goods.productlongname}}">
                        <p>{
     {
     ordergoods.o_goods.productlongname }}</p>
                        <p class="presentPrice">{
     {
     ordergoods.o_goods.price}}</p>
                    </a>
                    <section>

                        <span>{
     {
     ordergoods.o_goods_num}}</span>

                    </section>

                </li>
                {
     % endfor %}
            </ul>
        </li>
        {
     % endfor%}
    </ol>
</div>
{
     % endblock %}

最后再写一下已支付订单的view和url

def payed(request):
    order_id=request.GET.get('orderid')
    order=Order.objects.get(pk=order_id)
    order.o_status=ORDER_STATUS_NOT_SEND
    order.save()
    data={
     
        'status':200,
        'msg':'payed success',
    }
    return JsonResponse(data)
url(r'^payed/',views.payed,name='payed'),

你可能感兴趣的:(web开发,django,web)