Example #1
0
def product_created(request):
    form = ProductForm(request.POST)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect('/home')
    else:
        return render(request, 'new_product.html', {'form': ProductForm()})
Example #2
0
def product_create(request):
    form = ProductForm(request.POST)
    if form.is_valid():
        form.save()
        return redirect('products')
    else:
        context = {'form': form}
        return render(request, 'products/new.html', context)
Example #3
0
def create_product(request):
    form = ProductForm(request.POST)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect('/')
    else:
        return render(request, 'new.html', {
            'form': form
        })
Example #4
0
def edit_product(request, product_id):
    product = Product.objects.get(pk=product_id)
    form = ProductForm(request.POST, instance=product)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect('/')
    else:
        return render(request, 'edit.html', {
            'form': form,
            'product': product
        })
Example #5
0
def create_product(request):
    if request.method == "POST":
        form = ProductForm(request.POST)
        if form.is_valid():
            new_product = form.save()
            return HttpResponseRedirect('/products/' + str(new_product.pk))
        else:
            print(form.errors)
    else:
        form = ProductForm()
    return render(request, 'newproduct.html', {'form': form})
Example #6
0
def product_update(request, product_id):
    product = Product.objects.get(id=product_id)
    form = ProductForm(request.POST, instance=product)
    if form.is_valid():
        form.save()
        return redirect('product_show', product.id)
    else:
        context = {
            'form': form,
            'title': f'Edit {product.name} Information',
            'product': product,
        }
        response = render(request, 'products/edit.html', context)
        return HttpResponse(response)
Example #7
0
def update_product(request, id):

    product = get_object_or_404(Product, pk=id)
    if request.method == "POST":
        form = ProductForm(request.POST, instance=product)
        if form.is_valid():
            # import ipdb; ipdb.set_trace()
            product = form.save(commit=False)
            product.name = request.POST['name']
            product.price = request.POST['price']
            product.description = request.POST['description']
            product.save()
            return render(request, 'product.html', {'product': product})
    else:
        form = ProductForm(instance=product)
    return render(request, 'products/' + str(id) + '/edit/', {'form': form})