def get(self, request, pk=None): if not pk: form = CreateForm() else: pic = get_object_or_404(Pic, id=pk, owner=self.request.user) form = CreateForm(instance=pic) ctx = {'form': form} return render(request, self.template, ctx)
def post(self, request, pk=None): pic = get_object_or_404(Pic, id=pk, owner=self.request.user) form = CreateForm(request.POST, request.FILES or None, instance=pic) if not form.is_valid(): ctx = {'form': form} return render(request, self.template_name, ctx) pic = form.save(commit=False) pic.save() return redirect(self.success_url)
def post(self, request, pk=None): form = CreateForm(request.POST, request.FILES or None) if not form.is_valid(): ctx = {'form': form} return render(request, self.template_name, ctx) # Add owner to the model before saving pic = form.save(commit=False) pic.owner = self.request.user pic.save() return redirect(self.success_url)
def get(self, request, pk=None): form = CreateForm() ctx = {'form': form} return render(request, self.template_name, ctx)