def trade_add(request): goods_top = Trade.objects.values('goods__id','goods__title').annotate(goods_count=Count('goods')).order_by('-goods_count')[:10] price1 = 0 results = [] if request.method == 'POST': # If the form has been submitted... form = TradeForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data # ... #return HttpResponseRedirect('/thanks/') # Redirect after POST #results = GClass.objects.filter(title__icontains=form.cleaned_data['goodstitle']) isOldTrade = False if int(form.cleaned_data["trade_pk"]) > 0: isOldTrade = True shop1 = None if int(form.cleaned_data["shop_pk"]) > 0: shop1 = Shop.objects.get(pk=form.cleaned_data["shop_pk"]) else: shop1 = Shop(title=form.cleaned_data["shop"], type='mag') shop1.save() #gclass1 = None #if int(form.cleaned_data["gclass_pk"]) > 0: # gclass1 = GClass.objects.get(pk=form.cleaned_data["gclass_pk"]) #else: # gclass1 = GClass(title=form.cleaned_data["gclass"], section=GSection.objects.get(pk=1)) #TODO GSection # gclass1.save() goods1 = None if int(form.cleaned_data["gtitle_pk"]) > 0: goods1 = Goods.objects.get(pk=form.cleaned_data["gtitle_pk"]) goods1.title = form.cleaned_data["gtitle"] goods1.ed = form.cleaned_data["ed"] #goods1.gclass = gclass1 goods1.save() else: goods1 = Goods(title=form.cleaned_data["gtitle"], ed=form.cleaned_data["ed"]) goods1.save() price1 = "%.2f" % ( float(form.cleaned_data['cost']) / float(form.cleaned_data['amount']) ) if isOldTrade: trade1 = Trade.objects.get(pk=form.cleaned_data["trade_pk"]) else: trade1 = Trade() trade1.user = request.user trade1.shop = shop1 trade1.goods = goods1 trade1.time = form.cleaned_data["time"] trade1.amount = form.cleaned_data["amount"] trade1.price = price1 trade1.cost = form.cleaned_data["cost"] trade1.currency = form.cleaned_data["currency"] trade1.spytrade = form.cleaned_data["spytrade"] trade1.save() return HttpResponseRedirect("/") else: data = {'time': datetime.datetime.now, 'trade_pk': '0', 'shop_pk': '0', 'gclass_pk': '0', 'gtitle_pk': '0' } form = TradeForm(initial=data) # An unbound form return render_to_response('trade_add.html', {'price': price1, 'results': results, 'form': form, 'goods_top': goods_top}, context_instance=RequestContext(request))