示例#1
0
def product(request, product_id=0):
    ProductForm = forms.form_for_model(Product)
    title = 'Add product'
    if product_id:
        product = get_object_or_404(Product, pk=product_id)
        ProductForm = forms.form_for_instance(product)
        title = 'Editing product'
    if request.method == 'POST':
        form = ProductForm(request.POST.copy())
        if form.is_valid():
            p = form.save()
            return HttpResponseRedirect('%s/%i/'%('/warehouse/product', p.id))
    else:
        form = ProductForm()
    return render_to_response(
        'warehouse/product.html',
        {'form': form,
         'title': title},
        context_instance=RequestContext(request)
        )
示例#2
0
def product_bad(request, product_id=0):
    title = 'Add product'
    action = 'add'
    if product_id:
        product = get_object_or_404(Product, pk=product_id)
        form = ProductForm(product.__dict__)
        form.fields['category'].initial = '1'
        title = 'Editing product'
        action = 'update'
    else:
        form = ProductForm()
    if request.method == 'POST':
        category = Category.objects.get(pk=request.POST['category'])
        if request.POST['action'] == 'add':
            form = ProductForm(request.POST.copy())
            if form.is_valid():
                new = Product(description=form.clean_data['description'],
                              detail=form.clean_data['detail'],
                              category=category,
                              cost=form.clean_data['cost'],
                              quantity=form.clean_data['quantity'])
                new.save()
        if request.POST['action'] == 'update':
            if form.is_valid():
                for field in ['description', 'detail', 'cost', 'quantity']:
                    setattr(product, field, form.clean_data[field])
                product.category = category
                product.save()
        return HttpResponseRedirect(
            '%s/%i/' % ('/warehouse/product', request.POST['category'])
            )
    return render_to_response(
        'warehouse/product.html',
        {'form': form,
         'title': title,
         'action': action},
        context_instance=RequestContext(request)
        )