def new_item(request, template_name='jizhang/new_item.html'): if request.method == 'POST': form = ItemForm(request,data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect("/jizhang") else: form = ItemForm(request,initial={'pub_date':timezone.now().date()}) return render(request, template_name,{'form':form})
def new_item(request, template_name='jizhang/new_item.html'): if request.method == 'POST': form = ItemForm(request, data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect("/jizhang") else: form = ItemForm(request, initial={'pub_date': timezone.now().date()}) return render(request, template_name, {'form': form})
def edit_item(request, pk, template_name='jizhang/new_item.html'): if request.method == 'POST': form = ItemForm(request,data=request.POST) if form.is_valid(): form.save(pk) return HttpResponseRedirect("/jizhang") else: item_list = get_object_or_404(Item, id=pk) form = ItemForm(request,instance=item_list) return render(request, template_name,{'form':form})
def edit_item(request, pk, template_name='jizhang/new_item.html'): if request.method == 'POST': form = ItemForm(request, data=request.POST) if form.is_valid(): form.save(pk) return HttpResponseRedirect("/jizhang") else: item_list = get_object_or_404(Item, id=pk) form = ItemForm(request, instance=item_list) return render(request, template_name, {'form': form})
def new_item(request): last_save_item="" if request.method == 'POST': return_list=request.POST.get('return_list') add_another=request.POST.get('add_another') form = ItemForm(request,data=request.POST) if form.is_valid(): new_item = form.save() new_item.save() if not return_list: #继续新建 form = ItemForm(request,initial={'pub_date':timezone.now().date()}) if new_item.category.isIncome: isIncome=u"收入" else: isIncome=u"支出" last_save_item=u'您刚提交的"'+new_item.category.name+u'"分类下"'+str(new_item.price)+u'"元"'+isIncome+u"已保存" else: #返回列表 return HttpResponseRedirect("/jizhang") else: form = ItemForm(request,initial={'pub_date':timezone.now().date()}) most_used_categorys = Category.objects.filter(user__username=request.user.username).annotate(num_items=Count('item')).filter(num_items__gt=0).order_by('-num_items')[:8] context = {'last_save_item':last_save_item,'form':form,'username':request.user.username,'most_used_categorys':most_used_categorys} return render_to_response('jizhang/new_item.html',RequestContext(request,context))
def item(request,pk): if request.method == 'POST': form = ItemForm(request,data=request.POST) if form.is_valid(): new_item = form.save() new_item.id=pk new_item.save() return HttpResponseRedirect("/jizhang") else: item_list = get_object_or_404(Item, id=pk) form = ItemForm(request,instance=item_list) most_used_categorys = Category.objects.filter(user__username=request.user.username).annotate(num_items=Count('item')).filter(num_items__gt=0).order_by('-num_items')[:6] context = {'form':form,'username':request.user.username,'most_used_categorys':most_used_categorys} return render_to_response('jizhang/new_item.html',RequestContext(request,context))