Ejemplo n.º 1
0
    def post(self, request, pk=None):
        if pk is None:
            form = CreateForm(request.POST, request.FILES or None)

            if not form.is_valid():
                ctx = {'form': form}
                return render(request, self.template, ctx)

            # Add owner to the model before saving
            auto = form.save(commit=False)
            auto.owner = self.request.user
            auto.save()
            return redirect(self.success_url)
        else:
            auto = get_object_or_404(Auto, id=pk, owner=self.request.user)
            form = CreateForm(request.POST,
                              request.FILES or None,
                              instance=auto)

            if not form.is_valid():
                ctx = {'form': form}
                return render(request, self.template, ctx)

            auto.save()
            return redirect(self.success_url)
Ejemplo n.º 2
0
    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, 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)
Ejemplo n.º 3
0
    def post(self, request, pk=None):
        if not pk:
            form = CreateForm(request.POST, request.FILES or None)
        else:
            pic = get_object_or_404(Auto, 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, ctx)

        # Adjust the model owner before saving
        pic = form.save(commit=False)
        pic.owner = self.request.user
        pic.save()
        return redirect(self.success_url)