Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

python - Django success url using kwargs

I am trying to amend my get_success_url so that if any kwargs have been passed to it, I can build the returned url using them.

Heres what I have so far:

class CalcUpdate(SuccessMessageMixin, UpdateView):
    model = Calc
    template_name = 'calc/cru_template.html'
    form_class = CalcForm

    def archive_calc(self, object_id):
        model_a = Calc.objects.get(id = object_id)
        model_b = Calc()

        for field in model_a._meta.fields:
            setattr(model_b, field.name, getattr(model_a, field.name))
        model_b.pk = None
        model_b.save()

        self.get_success_url(idnumber = model_b.pk)

    def form_valid(self, form):
        #objects
        if self.object.checked == True:
            object_id = self.object.id
            self.archive_calc(object_id)
        #save

    def get_success_url(self, **kwargs):         
        if  kwargs != None:
            return reverse_lazy('detail', kwargs = {'pk': kwargs['idnumber']})
        else:
            return reverse_lazy('detail', args = (self.object.id,))

So far this just gives a keyerror detailing 'idnumber'.

I have printed kwargs['idnumber'] and it returns the pk as expected however I just cant seem to see where I am going wrong with this.

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

form_valid should return a HttpResponseRedirect https://github.com/django/django/blob/master/django/views/generic/edit.py#L57 which in your case, you never do. I dont know if you have any code after #save, but take a look at the comments I made in your code

class CalcUpdate(SuccessMessageMixin, UpdateView):
    model = Calc
    template_name = 'calc/cru_template.html'
    form_class = CalcForm

    def archive_calc(self, object_id):
        model_a = Calc.objects.get(id = object_id)
        model_b = Calc()

        for field in model_a._meta.fields:
            setattr(model_b, field.name, getattr(model_a, field.name))
        model_b.pk = None
        model_b.save()

        return self.get_success_url(idnumber = model_b.pk) # you never return this value

    def form_valid(self, form):
        #objects
        if self.object.checked == True:
            object_id = self.object.id
            return HttpResponseRedirect(self.archive_calc(object_id)) # you never return a `HttpResponse`
        #save  -- If this is where you are saving... you can store the value from archive and return it after saving

    def get_success_url(self, **kwargs):         
        if  kwargs != None:
            return reverse_lazy('detail', kwargs = {'pk': kwargs['idnumber']})
        else:
            return reverse_lazy('detail', args = (self.object.id,))

Also you don't need to manually copy the fields, just do (assuming there are no unique constraints because if there were, your version would fail too):

    def archive_calc(self, object_id):
        c = self.model.objects.get(id = object_id)
        c.pk = None
        c.save()

        return self.get_success_url(idnumber = c.pk)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...