Django使用kwargs的成功网址

时间:2014-11-12 21:36:32

标签: python django

我正在尝试修改我的get_success_url,以便如果已经传递了kwargs,我可以使用它们构建返回的网址。

到目前为止我所拥有的:

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,))

到目前为止,这只是keyerror详细说明'idnumber'

我已打印kwargs['idnumber']并按预期返回pk但是我似乎无法看到我在哪里出错。

提前致谢。

2 个答案:

答案 0 :(得分:6)

form_valid应该返回HttpResponseRedirect https://github.com/django/django/blob/master/django/views/generic/edit.py#L57,在您的情况下,您永远不会这样做。我不知道你在#save之后是否有任何代码,但请查看我在你的代码中发表的评论

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,))

此外,您不需要手动复制字段,只需这样做(假设没有unique约束,因为如果有,您的版本也会失败):

    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)

答案 1 :(得分:1)

在玩了@Ngenator的答案和其他各种帖子后,我有以下工作代码。然而,看起来不是很好:(

def get_success_url(self):
    if self.pknumber != None:
        return reverse_lazy('pstdetail', args = (self.pknumber,))
    else:
        return reverse_lazy('pstdetail', args = (self.object.id,))

我在视图中的必要位置有self.pknumber = model_b.pk,而self.pknumber = None其他地方在哪里启用if语句来构建所需的url。希望这可以帮助任何人,并随时指出任何错误/改进。