Django
常用操作
Django生产设置
Setting设置
装饰器
类
类--验证登陆
ListView
CreateView
DeleteView
URL方法
Views方法
model数据模型
models
admin
views
url
model查询操作
model聚合查询
model列操作及复杂查询
登录退出
登陆成功后跳转指定页面
修改密码
重置密码
分页
标签
HTML文件
开启HTTPS
CSRF
本文档使用MrDoc发布
返回首页
-
+
Views方法
2019年10月16日 14:29
admin
#跳转 from django.http import HttpResponse from dfango.urls import reverse def bak(request): return HttpResponseRedirect( reverse("account:user_login") ) #跳转页 from django.http import HttpResponse,JsonResponse @login_required def create(request): return render(request,'./pages/databases/create.html') def edit(request,id): phone_info = models.sign_phone.objects.get(id=id) return render(request,'./pages/databases/edit.html',{ 'phone_info': phone_info }) #显示页 from django.http import HttpResponse,JsonResponse @login_required def hello(request): return HttpResponse("Hello world ! ") #处理get请求,返回json #处理json中时间格式的数据 class DateEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj,datetime.datetime): return obj.strftime("%Y-%m-%d %H:%M:%S") else: return json.JSONEncoder.default(self,obj) @csrf_exempt def api_data(request): check_phone = request.GET.get('phone') page = request.GET.get('page') limit = request.GET.get('limit') if check_phone is not None: api_data_count = models.sign_phone.objects.filter(phone__contains=check_phone).count() api_data = models.sign_phone.objects.filter(phone__contains=check_phone).order_by("-id") else: api_data_count = models.sign_phone.objects.count() api_data = models.sign_phone.objects.all().order_by("-id") json_list = [] i = 0 start_num = (int(page)-1) * int(limit) end_num = (int(page)-1) * int(limit) + int(limit) for data in api_data: if i >= start_num and i < end_num : json_dict = {} json_dict["id"] = data.id json_dict["phone"] = data.phone json_dict["count"] = data.count json_dict["type"] = data.type json_dict["mono"] = data.mono json_dict["update_time"] = data.update_time json_list.append(json_dict) i = i + 1 data = { "code": 0, "count": api_data_count, "success":"true", "msg":"", "data": json_list } return HttpResponse(json.dumps(data,cls=DateEncoder),content_type="application/json") #处理post请求 @csrf_exempt def delete_api(request): phone_id = request.POST.get('idsStr').split(',') for delete_id in phone_id: if delete_id != '': print(delete_id) models.sign_phone.objects.filter(id=delete_id).delete() return render(request,'./pages/databases/datatables.html') #处理上传的excel @csrf_exempt def file_upload_api(request): if request.method == "POST": print(request) f = request.FILES['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] print(table) nrows = table.nrows # 行数 print(nrows) # ncole = table.ncols # 列数 try: for i in range(1, nrows): rowValues = table.row_values(i) # 一行的数据 upload_phone = rowValues[0] upload_mono = rowValues[1] print(upload_phone) print(upload_mono) dir = {"phone":upload_phone,"mono":upload_mono} phone_status = models.sign_phone.objects.filter(phone=upload_phone).count() if phone_status > 0: print("已有号码") models.sign_phone.objects.filter(phone=upload_phone).update(**dir) else: print("新号码") models.sign_phone.objects.create(**dir) except Exception as e: return HttpResponse(e) return JsonResponse({'msg':'ok'}) return JsonResponse({'msg':'上传文件格式不是xlsx'}) return JsonResponse({'msg':'不是post请求'})
分享到: