class StoreEditView(TemplateView): template_name = 'store/store_edit.html' @method_decorator(permission_required('store.change_store', raise_exception=True)) def dispatch(self, request, *args, **kwargs): if kwargs['pk'] != 'new': self.instance = get_object_or_404(Store, pk=kwargs['pk']) else: self.instance = None self.form = StoreEditForm(request.POST or None, instance=self.instance) return super(StoreEditView, self).dispatch(request, *args, **kwargs) def get_context_data(self, **kwargs): context = super(StoreEditView, self).get_context_data(**kwargs) context['form'] = self.form return context def post(self, request, *args, **kwargs): if self.form.is_valid(): saved_instance = self.form.save() if not self.instance: messages.success(request, u'The store created successfully') else: messages.success(request, u'The store saved successfully') return redirect('store', pk=saved_instance.pk) return self.get(request, *args, **kwargs)
def dispatch(self, request, *args, **kwargs): if kwargs['pk'] != 'new': self.instance = get_object_or_404(Store, pk=kwargs['pk']) else: self.instance = None self.form = StoreEditForm(request.POST or None, instance=self.instance) return super(StoreEditView, self).dispatch(request, *args, **kwargs)