def product(request, pid=None): if request.user and request.user.is_staff: # show an update screen if product is given, otherwise insert try: product = Product.objects.get(id=pid) prform = ProductForm({ 'name': product.name, 'price': product.price, 'description': product.description, 'quantity': product.quantity }) operation = 'update' except: #insert case prform = ProductForm() operation = 'insert' return render(request, 'productupd.html', { 'pid': pid, 'form': prform, 'operation': operation }) else: try: product = Product.objects.get(id=pid) return render(request, 'productview.html', {'product': product}) except: return render(request, 'productview.html', {'product': None})
def new_product(request): if request.method == "POST": nowy = Product(timestamp=timezone.now()) form1 = ProductForm(instance=nowy, data=request.POST) if form1.is_valid(): form1.save() return redirect("index") return redirect("shop_admin")
def product_edit(): data = request.args.to_dict() if 'id' not in data: return redirect(url_for('index')) form = ProductForm() if form.validate_on_submit(): pass return render_template('shop/product/edit.html', product_form=form)
def add_products(request, template_name='shop/add_products.html'): if request.method == 'POST': form = ProductForm(request.POST) if form.is_valid(): form.save(request.user) return HttpResponseRedirect(reverse('show_products')) else: form = ProductForm() context = RequestContext(request) return render_to_response(template_name, {'form': form}, context_instance=context)
def update_product(request,pk): product = get_object_or_404(Product,pk = pk) form = ProductForm(request.POST or None,request.FILES or None,instance = product) if form.is_valid(): product = form.save(commit=False) image = form.cleaned_data['image'] product.image = image product.save() return redirect('shop:homepage') return render(request,'shop/product_update.html',{ 'form':form })
def add_product(): categories = Categories.query.all() form = ProductForm() if request.method == 'POST': product = Products(category_name=form.category_name.data, product_name=form.product_name.data, quantity=form.quantity.data, price=form.price.data, age_recommendation=form.age_recommendation.data, dimensions=form.dimensions.data, country_of_design=form.country_of_design.data, country_of_manufacture=form.country_of_design.data, primary_material=form.primary_material.data, assembly_required=form.assembly_required.data, gift_wrap=form.gift_wrap.data, image_product=photos.save( request.files.get('image_product'))) db.session.add(product) db.session.commit() flash(f'Your product has been created!', 'success') return redirect(url_for('add_product')) return render_template("staff/add_product.html", title="staff", form=form, categories=categories)
def add_product(): form = ProductForm() if form.validate_on_submit(): picture_file = save_picture(form.picture.data) product = Product(p_name=form.p_name.data, description=form.description.data, price=form.price.data, image=picture_file) db.session.add(product) db.session.commit() flash('Product has been added!', 'success') return redirect(url_for('home')) return render_template('add_product.html', title='Add Product', form=form, legend='Add Product')
def productadd(request): if request.method == 'POST': form = ProductForm(request.POST, request.FILES) if form.is_valid(): form.save() return redirect('/products') else: form = ProductForm() return render(request, 'shop/productadd.html', {'form': form})
def add_product(request): product_instance = Product() if request.method == "POST": form = ProductForm(request.POST) if form.is_valid(): product_instance.category = Category.objects.get( id=form.data['category']) product_instance.name = form.data['name'] product_instance.price = form.data['price'] if form.data.__contains__('available'): product_instance.available = bool(form.data['available']) else: product_instance.available = False product_instance.stock = form.data['stock'] product_instance.description = form.data['description'] product_instance.image = form.data['image'] product_instance.slug = slugify(form.data['name']) product_instance.save() else: form = ProductForm() return render(request, 'shop/product/add_product.html', {'form': form})
def update_product(product_id): product = Product.query.get_or_404(product_id) form = ProductForm() if form.validate_on_submit(): product.p_name = form.p_name.data product.description = form.description.data product.price = form.price.data if form.picture.data: picture_file = save_picture(form.picture.data) product.image = picture_file db.session.commit() flash('Your product has been updated!', 'success') return redirect(url_for('product', product_id=product.id)) elif request.method == 'GET': form.p_name.data = product.p_name form.description.data = product.description form.price.data = product.price form.picture.data = product.image return render_template('add_product.html', title='Update', form=form, legend='Update')
def update_product(request,pid): if request.method=='GET': p = Product.objects.get(pk=pid) forms = ProductForm(instance=p) return render(request,'product_update.html',{'forms':forms}) else: p = Product.objects.get(pk=pid) forms = ProductForm(instance=p,data=request.POST) if forms.is_valid(): forms.save() return HttpResponseRedirect('/')
def show_product(request): if request.method == 'POST': forms = ProductForm(request.POST,request.FILES) print request.POST if forms.is_valid(): forms.save() return HttpResponseRedirect('/') else: forms = ProductForm() context_dict = {'forms':forms} return render (request,'product.html',context_dict)
def product_create_view(request): if request.method == "POST": form = ProductForm(request.POST, request.FILES) if form.is_valid(): form.save() return redirect('shop:product_list') else: form = ProductForm() return render(request, 'shop/product/add_product.html', {'form':form})
def update_product_by_id(product_id): categories = Categories.query.all() form = ProductForm() update_product = Products.query.get_or_404(product_id) if form.validate_on_submit(): update_product.product_name = form.product_name.data update_product.quantity = form.quantity.data update_product.price = form.price.data update_product.age_recommendation = form.age_recommendation.data update_product.dimensions = form.dimensions.data update_product.country_of_design = form.country_of_design.data update_product.country_of_manufacture = form.country_of_manufacture.data update_product.primary_material = form.primary_material.data update_product.assembly_required = form.assembly_required.data update_product.gift_wrap = form.gift_wrap.data update_product.image_product = photos.save( request.files.get('image_product')) update_product.update_date = datetime.datetime.utcnow() db.session.add(update_product) db.session.commit() flash(f"Product has been updated", "success") return redirect( url_for('view_all_products', product_id=update_product.id)) elif request.method == "GET": form.product_name.data = update_product.product_name form.quantity.data = update_product.quantity form.price.data = update_product.price form.age_recommendation.data = update_product.age_recommendation form.dimensions.data = update_product.dimensions form.country_of_design.data = update_product.country_of_design form.country_of_manufacture.data = update_product.country_of_manufacture form.primary_material.data = update_product.primary_material form.assembly_required.data = update_product.assembly_required form.gift_wrap.data = update_product.gift_wrap return render_template("staff/update_product.html", title="update product", form=form, categories=categories)
def product_form_view(request): form = ProductForm() if request.method == 'POST': form = ProductForm(request.POST) if form.is_valid(): form.save() return redirect(reverse_lazy('products')) else: pass return render(request, 'product_form.html', context={ 'form': form })
def product_form(request): if request.method =='POST': form = ProductForm(request.POST) if form.is_valid(): form.save() return redirect( 'Profile:product_form' ) else: form = ProductForm() #form = {'form': form} #args = {'form': form} return render(request, 'Profile/product_form.html',{'form': form} )
def product_new(request): if request.method == "POST": form = ProductForm(request.POST) if form.is_valid(): product = form.save(commit=False) product.created_date = timezone.now() product.save() return redirect('shop:product_detail', id=product.id) else: form = ProductForm() return render(request, 'shop/product_edit.html', {'form': form})
def product_edit(request, id): product = get_object_or_404(Product, id=id) if request.method == "POST": form = ProductForm(request.POST, instance=product) if form.is_valid(): product = form.save(commit=False) product.created_date = timezone.now() product.save() return redirect('shop:product_detail', id=product.id) else: form = ProductForm(instance=product) return render(request, 'shop/product_edit.html', {'form': form})
def product_new(request): if request.method == "POST": form = ProductForm(request.POST) if form.is_valid(): product = form.save(commit=False) product.created = timezone.now() product.save() products = Product.objects.filter(available=True) return render(request, 'portfolio/admin.html', {'products': products}) else: form = ProductForm() return render(request, 'portfolio/product_add.html', {'form': form})
def add_product(request): if request.method == 'POST': product_form = ProductForm( request.POST, request.FILES, ) if product_form.is_valid(): # save product to database product_form.save() messages.success(request, 'The product has been added.') # redirect to the dashboard return redirect(reverse('dashboard')) product_form = ProductForm() context = { 'product_form': product_form, } return render(request, 'add_product.html', context)
def create_product(request): if request.method == 'POST': form = ProductForm(request.POST,request.FILES) if form.is_valid(): product = form.save(commit=False) print(product) image = form.cleaned_data['image'] product.image = image product.save() return redirect('shop:homepage') else: return render(request,'shop/create_product.html',{ 'form':ProductForm() }) form = ProductForm() return render(request,'shop/create_product.html',{'form':form})
def employee_product_edit(request, pk): product = get_object_or_404(Product, pk=pk) print("I am here") if request.method == "POST": # update form = ProductForm(request.POST, instance=product) if form.is_valid(): product = form.save(commit=False) product.updated = timezone.now() product.save() products = Product.objects.filter(available=True) return render(request, 'portfolio/admin.html', {'products': products}) else: # edit print("I am here") form = ProductForm(instance=product) return render(request, 'portfolio/product_update.html', {'form': form})
def new_product(request, subcategory_pk): subcategory = get_object_or_404(SubCategory, pk=subcategory_pk) categories = Category.objects.order_by('-name') if request.method == 'POST': form = ProductForm(request.POST, request.FILES) if form.is_valid(): product = form.save(commit=False) product.sub_category = subcategory product.save() return redirect('frontdesk:categories') else: form = ProductForm() return render(request, 'frontdesk/categories.html', { 'form': form, 'subcategory': subcategory, 'categories': categories })
def create_item_View(request): """ Provide the ability to create item post """ form = ProductForm(request.POST or None) user = get_object_or_404(User, id=request.user.id) categories = Category.objects.all() if request.method == 'POST': if form.is_valid(): instance = form.save(commit=False) instance.user = user instance.save() # for save tags form.save_m2m() return redirect( reverse("ecomapp:single-item", kwargs={'id': instance.id})) context = {'form': form, 'categories': categories} return render(request, 'ecomapp/post-item.html', context)