def post(self, request, *args, **kwargs): """ 収集したデータを送信する endpoint:"listing/shuppin/" name: "" """ """テスト項目 """ user = User.objects.get(email="*****@*****.**") new_dict = request.data.copy() category = request.data["category_"] cate_obj = Category.objects.get(number=category) new_dict["category"] = cate_obj.id new_dict["point"] = GEOSGeometry(request.data["point_"]) new_dict["user"] = user # print(request.data) # print(new_dict) title = request.data["title"] price = request.data["price"] if len(Item.objects.filter(title=title).filter(price=price)) >= 1: # print("既に登録済み") return HttpResponse("登録済みなので登録しない") # print(request.FILES) form = ItemModelForm(new_dict) # print(form.is_valid()) # print([ele for ele in form.errors]) if form.is_valid(): obj = form.save(commit=False) adm1 = request.POST["adm1"] adm2 = request.POST["adm2"] obj.adm1 = adm1 obj.adm2 = adm2 obj.point = new_dict["point"] obj.radius = new_dict["radius"] obj.user = user try: obj.image1 = request.FILES["file0"] except: pass try: obj.image2 = request.FILES["file1"] except: pass try: obj.image3 = request.FILES["file2"] except: pass obj.save() return HttpResponse("Response")
def post(self, request, *args, **kwargs): """ endpoint:"items/create2/" name: "items:item_create2" """ """テスト項目 categoryを選択しない場合にはItemオブジェクトが作られない """ form = ItemModelForm(request.POST, request.FILES) if form.is_valid(): obj = form.save(commit=False) adm1 = request.POST["adm1"] adm2 = request.POST["adm2"] obj.adm1 = adm1 obj.adm2 = adm2 obj.save() try: wkt = request.POST["point"] result = is_in_Guatemala(wkt) if result is True: point = GEOSGeometry(wkt) obj.point = point elif result is False: messages.warning(request, no_match_guatemala_message) except: pass try: obj.radius = int(request.POST["radius"]) except: pass try: obj.image1 = request.FILES['image1'] except: pass try: obj.image2 = request.FILES['image2'] except: pass try: obj.image3 = request.FILES['image3'] except: pass # 現状では1つの記事につき画像は3枚まで使う仕様とする # try: # obj.image4 = request.FILES['image4'] # except: # pass # try: # obj.image5 = request.FILES['image5'] # except: # pass # try: # obj.image6 = request.FILES['image6'] # except: # pass obj.user = request.user obj.save() context = {} context["item_obj"] = obj return render(request, 'items/created.html', context) else: for error in form.errors: print(error) context = {} context["form"] = form return render(request, 'items/create_item_k.html', context)