Json Response 반환 사례

1555 단어 DjangoRESTframework
from django.shortcuts import render
from django.http import JsonResponse
import xlrd
from operatorsystem import models
from django.db import transaction

def index_view(request):
    return render(request, 'index.html')


def excel_upload(request):
    '''
    :param request:
    :return:     excel   ,     
    '''
    if request.method == "POST":

        f = request.FILES['my_file']
        type_excel = f.name.split('.')[1]
        if 'xlsx' == type_excel:
            #        excel  
            wb = xlrd.open_workbook(filename=None, file_contents=f.read())  #        
            table = wb.sheets()[0]
            nrows = table.nrows  #   
            # ncole = table.ncols  #   
            try:
                with transaction.atomic():
                    for i in range(1, nrows):
                        # if 4 == i:
                        #     i/0
                        rowValues = table.row_values(i)  #      
                        good = models.GoodsManage.objects.get(international_code=rowValues[0])
                        models.SupplierGoodsManage.objects.create(goods=good, sale_price=rowValues[1],sale_min_count = rowValues[2])
            except Exception as e:
                return JsonResponse({'msg':'    ....'})

            return JsonResponse({'msg':'ok'})

        return JsonResponse({'msg':'        xlsx'})

    return JsonResponse({'msg':'  post  '})

좋은 웹페이지 즐겨찾기