def product_create_view(request): form = ProductModelForm() success_url = reverse('products:list') if request.method == 'POST': form = ProductModelForm(data=request.POST) if form.is_valid(): form.save() return redirect(success_url) return render(request, 'products/create.html', {'form': form})
def create_product(request): form = ProductModelForm(request.POST or None) if form.is_valid(): form.save() return redirect("products:list") context = {"form": form} return render(request, "products/create.html", context=context)
def post(self, request): success_url = reverse_lazy('products:list') form = ProductModelForm(request.POST) if form.is_valid(): form.save() return redirect(success_url) return render(request, 'products/create.html', {'form': form})
def create_produtct(request): if request.method == 'POST': form = ProductModelForm(request.POST) if form.is_valid(): form.save() return redirect('products:list') else: form = ProductModelForm() context = {'form': form} return render(request, 'products/create.html', context=context)
def update_product(request, product_id): product = Product.objects.get(pk=product_id) form = ProductModelForm(data=request.POST or None, instance=product) if form.is_valid(): form.save() return redirect("products:list") context = {"form": form} return render(request, "products/update.html", context=context)
def product_update_view(request, pk): obj = get_object_or_404(Product, pk=pk) form = ProductModelForm(instance=obj) success_url = reverse('products:list') if request.method == 'POST': form = ProductModelForm(request.POST, files=request.FILES, instance=obj) if form.is_valid(): form.save() return redirect(success_url) return render(request, 'products/update.html', {'form': form})
def create_product(request): if request.method == 'POST': #salvar formulario form = ProductModelForm(request.POST) if form.is_valid(): form.save() return redirect('/products/list') else: #get formulario form = ProductModelForm(request.GET) context = {'form': form} return render(request, 'products/Create.html', context=context)
def create_product(request): if request.method == 'POST': #salvar form, trazendo formulário para criação de produto form = ProductModelForm(request.POST) if form.is_valid(): #True -> é valido form.save() form = ProductModelForm() else: # Get form form = ProductModelForm() context = {'form': form} return render(request, 'products/create.html', context=context)
def update_product(request, product_id): product = Product.objects.get(pk=product_id) if request.method == 'POST': form = ProductModelForm(data=request.POST, instance=product) if form.is_valid(): form.save() return redirect('products:list') else: form = ProductModelForm(instance=product) context = {'form': form} return render(request, 'products/update.html', context=context)
def product_create(request): title = 'Добавить товар' template_name = 'products/create.html' success_url = reverse_lazy('products:catalog') form = ProductModelForm(request.POST) if request.method == 'POST': if form.is_valid(): form.save() return redirect(success_url) context = {'form': form, 'title': title, 'menu': get_menu()} return render(request, template_name, context)
def create_product(request): if request.method == 'POST': #Salvar form form = ProductModelForm(request.POST) if form.is_valid(): #True -> é valido form.save() return redirect('products:list') #Retorna para a lista de produtos else: #Não valido #Get form form = ProductModelForm() context = { #Sempre é um dicionário 'form': form } return render(request, 'products/create.html', context=context)
def product_model_create(request): template_write_name = 'Create product' template_name = 'products/create.html', success_url = reverse_lazy('products:product_list') form = ProductModelForm(request.POST) if request.method == 'POST': if form.is_valid(): form.save() return redirect(success_url) return render(request, template_name, { 'form': form, 'template_write_name': template_write_name })
def update_product(request, product_id): product = Product.objects.get(pk=product_id) if request.method == 'POST': form = ProductModelForm(data=request.POST, instance=product) if form.is_valid(): #True -> é valido form.save() return redirect('products:list') else: #Não valido #Get form form = ProductModelForm(instance=product) #id = instancia context = { #Sempre é um dicionário 'form': form } return render(request, 'products/update.html', context=context)
def create_product(request): if request.method == 'POST': # Salvar form form = ProductModelForm(request.POST) if form.is_valid(): # True -> é válido form.save() return redirect( 'products:list') # retorna para a página de lista de produtos else: # Get Form form = ProductModelForm() # form = ProductModelForm() context = {'form': form} return render(request, 'products/create.html', context=context)
def product_create_view(request, *args, **kwargs): form = ProductModelForm(request.POST or None) if form.is_valid(): obj = form.save(commit=False) obj.user = request.user obj.save() form = ProductModelForm() return render(request, "forms.html", {"form": form})
def product_model_update(request, pk): template_write_name = 'Update product' template_name = 'products/create.html', success_url = reverse_lazy('products:product_list') obj = get_object_or_404(Product, pk=pk) form = ProductModelForm(instanse=obj) if request.method == 'POST': form = ProductModelForm(request.POST, instanse=obj) if form.is_valid(): form.save() return redirect(success_url) return render(request, template_name, { 'form': form, 'template_write_name': template_write_name })
def product_update(request, pk): title = 'Редактировать товар' template_name = 'products/create.html' success_url = reverse_lazy('products:catalog') obj = get_object_or_404(Product, pk=pk) form = ProductModelForm(instance=obj) if request.method == 'POST': form = ProductModelForm(request.POST, instance=obj) if form.is_valid(): form.save() return redirect(success_url) context = {'form': form, 'title': title, 'menu': get_menu()} return render(request, template_name, context)
def create_view(request): form = ProductModelForm(request.POST or None, request.FILES or None) if not form.is_valid(): print('---FORM-NOT-VALID---') print(form.errors) else: if form.is_valid(): instance = form.save(commit=False) instance.sale_price=instance.price instance.save() form.save() template="form.html" context={ "form":form, "submit_btn":"Create Product", } return render(request, template, context)
def product_update(request, pk): success_url = reverse_lazy('products:list') obj = get_object_or_404(Product, id=pk) form = ProductModelForm(instance=obj) if request.method == 'POST': form = ProductModelForm(request.POST, instance=obj) if form.is_valid(): form.save() return redirect(success_url) return render(request, 'products/create.html', {'form': form})
def update_product(request, product_id): product = Product.objects.get(pk=product_id) if request.method == 'POST': #salvar form, trazendo dados instaciados em product para o formulário da tela. form = ProductModelForm(data=request.POST, instance=product) if form.is_valid(): form.save() return redirect('products:list') else: # GET # Trazendo dados instanciados em product para o formulário. form = ProductModelForm(instance=product) context = { 'form': form, } return render(request, 'products/update.html', context=context)
def update_view(request, object_id=None): product = get_object_or_404(Product, id=object_id) form = ProductModelForm(request.POST or None, instance=product) if not form.is_valid(): print('---FORM-NOT-VALID---') print(form.errors) else: if form.is_valid(): instance=form.save(commit=False) instance.save() template = "form.html" context = { "object":product, "form":form, "submit_btn":"Update Product", } return render(request, template, context)