def update(request, id = -1): try: item = Item.objects.get(pk=id) except ObjectDoesNotExist: raise Http404 # POST flag discriminates submission from form request if request.method == 'POST': form = ItemCreate(request.POST, request.FILES, instance=item) if form.is_valid(): form.save() return { 'success': True, 'message': "Item '%s' updated" % form.instance.name } # Form was invalid return { 'success': False, 'errors': form.errors } else: # Create a form based on the instance of the customer found form = ItemCreate(instance=item) return render_to_response('item/update.html', {'form': form}, context_instance=RequestContext(request))
def create(request): # POST flag discriminates submission from form request if request.method == 'POST': form = ItemCreate(request.POST, request.FILES) if form.is_valid(): form.save() transaction = Transaction(id=int(''.join(str(randrange(10)) for i in range(9))), action=u'l', customer=form.cleaned_data['owner'], item=form.instance, signoff=request.user) transaction.save() return { 'success': True, 'message': "Item '%s' created" % form.instance.name } # Form was invalid return { 'success': False, 'errors': form.errors } else: # Just show the form to the user form = ItemCreate() return render_to_response('item/create.html', {'form': form}, context_instance=RequestContext(request))