def post(self, request, *args, **kwargs): """ Handles POST requests, instantiating a form instance with the passed POST variables and then checked for validity. Things to do: - Check if all forms are valid - Save the pizza [- Save the topping] - Set topping and pizza PK to ToppingUsage - Save toppingUsage """ self.get_instance(args, kwargs) self.pizza_form = PizzaForm(data=self.request.POST, instance=self.pizza) self.topping_usage_formset = ToppingUsageFormSet(data=self.request.POST, instance=self.pizza) if self.pizza_form.is_valid() and self.topping_usage_formset.is_valid(): # save pizza self.pizza = self.pizza_form.save() # Set pizza instance for toppingusageset before saving it # This is only necessary for creating new pizza, but should not harm anyway... self.topping_usage_formset.instance = self.pizza # Save toppingusageset self.topping_usage_formset.save() # return pizza url return HttpResponseRedirect(self.pizza.get_absolute_url()) else: return self.render_to_response(self.get_context_data())
def order(request): multiple_form = MultiplePizzaForm() if request.method == 'POST': filled_form = PizzaForm(request.POST) if filled_form.is_valid(): created_pizza = filled_form.save() created_pizza_pk = created_pizza.id note = 'Your order is succesfully done! You have order topping1:{},topping2:{} with size:{}'.format( filled_form.cleaned_data['topping1'], filled_form.cleaned_data['topping2'], filled_form.cleaned_data['size']) filled_form = PizzaForm() else: created_pizza_pk = None note = 'Your pizzas order failed!' return render( request, 'pizza/order.html', { 'created_pizza_pk': created_pizza_pk, 'pizzaform': filled_form, 'note': note, 'mutliple_form': multiple_form }) else: form = PizzaForm() return render(request, 'pizza/order.html', { 'pizzaform': form, 'multiple_form': multiple_form })
def dodajPizza(request): if request.method == 'POST': pass #form = PizzaForm(request.POST) #if form.is_valid(): # print(form.cleaned_data) else: form = PizzaForm() return render(request, 'pizza/pizzaform.html', {'form': form})
def get(self, request, *args, **kwargs): """ Handles GET requests and instantiates a blank version of the forms. Should display: - Pizzaform to create a new pizza - Set of Toppingusages to specify the usage parameters of a topping - Possibility to create new topping on-the-fly """ self.get_instance(args, kwargs) self.pizza_form = PizzaForm(instance = self.pizza) self.topping_usage_formset = ToppingUsageFormSet(instance=self.pizza) return self.render_to_response(self.get_context_data())
def edit_order(request, pizza_id): pizza = Pizza.objects.get(id=pizza_id) form = PizzaForm(instance=pizza) if request.method == 'POST': filled_form = PizzaForm(request.POST, instance=pizza) if filled_form.is_valid(): filled_form.save() form = filled_form note = 'Order has been updated' return render(request, 'pizza/edit_order.html', { 'pizzaform': form, 'pizza': pizza, 'note': note }) return render(request, 'pizza/edit_order.html', { 'pizzaform': form, 'pizza': pizza})
def order(request): multiple_form = MultiplePizzaForm() if request.method == 'POST': filled_form = PizzaForm(request.POST) if filled_form.is_valid(): created_pizza = filled_form.save() created_pizza_id = created_pizza.id note = 'Thanks for ordering! Your %s, %s and %s Pizza is on its way.' %(filled_form.cleaned_data['size'], filled_form.cleaned_data['topping1'], filled_form.cleaned_data['topping2']) filled_form = PizzaForm() else: created_pizza_id = None note = 'Pizza order has failed. Try Again' return render(request, 'pizza/order.html', {'pizzaform': filled_form, 'note': note, 'multiple_form': multiple_form, 'created_pizza_id': created_pizza_id }) else: form = PizzaForm() return render(request, 'pizza/order.html', {'pizzaform': form, 'multiple_form': multiple_form })
class PizzaEdit(TemplateView): template_name = 'pizza/pizza_form.html' def get_instance(self, *args, **kwargs): """ In case we are editing an existing pizza there should be a primary key provided. Try to get that pizza, raise an error if not available. """ self.pizza = None pk = self.kwargs.get('pk', None) if pk: try: self.pizza = Pizza.objects.get(pk=pk) except ObjectDoesNotExist: raise Http404("No %(verbose_name)s found matching the query" % {'verbose_name': Pizza._meta.verbose_name}) def get(self, request, *args, **kwargs): """ Handles GET requests and instantiates a blank version of the forms. Should display: - Pizzaform to create a new pizza - Set of Toppingusages to specify the usage parameters of a topping - Possibility to create new topping on-the-fly """ self.get_instance(args, kwargs) self.pizza_form = PizzaForm(instance = self.pizza) self.topping_usage_formset = ToppingUsageFormSet(instance=self.pizza) return self.render_to_response(self.get_context_data()) def post(self, request, *args, **kwargs): """ Handles POST requests, instantiating a form instance with the passed POST variables and then checked for validity. Things to do: - Check if all forms are valid - Save the pizza [- Save the topping] - Set topping and pizza PK to ToppingUsage - Save toppingUsage """ self.get_instance(args, kwargs) self.pizza_form = PizzaForm(data=self.request.POST, instance=self.pizza) self.topping_usage_formset = ToppingUsageFormSet(data=self.request.POST, instance=self.pizza) if self.pizza_form.is_valid() and self.topping_usage_formset.is_valid(): # save pizza self.pizza = self.pizza_form.save() # Set pizza instance for toppingusageset before saving it # This is only necessary for creating new pizza, but should not harm anyway... self.topping_usage_formset.instance = self.pizza # Save toppingusageset self.topping_usage_formset.save() # return pizza url return HttpResponseRedirect(self.pizza.get_absolute_url()) else: return self.render_to_response(self.get_context_data()) def get_context_data(self, **kwargs): # Call the base implementation first to get a context context = super(PizzaEdit, self).get_context_data(**kwargs) # Add extra data context['pizza'] = self.pizza context['pizza_form'] = self.pizza_form context['topping_usage_formset'] = self.topping_usage_formset context['toppings'] = Topping.objects.all() return context