Django Generic查看UpdateView重定向URL和更新的slug

时间:2015-10-14 02:21:09

标签: django django-forms django-views django-generic-views

这是我的更新视图,由带有slug字段的URL调用,例如

/mymodel/update/<model_name_slug>/

MyModel有两个字段nameslug。段塞字段<model_name_slug>是从MyModel.name自动生成的。如果用户更新它,将自动生成一个新的段塞字段。然后,我想用新生成的段塞字段重定向到这个新URL。

slug字段自动生成正在运行。它在MyModelEditForm中实现。但是,下面的代码不起作用。原因是:

1)用户输入此URL以更新现有模型

/mymodel/update/example-model

2)用户将Name属性更改为"example model changed"。然后,slug字段将在"example-model-changed"中生成为MyModel

3)但是网址未重定向到"/mymodel/update/example-model-changed",因为get_object()将返回None. get()将无法将新生成的网址"example-model-changed"与原始{ {1}}

我在下面的代码中缺少什么?我尝试使用"example-model"访问新的更新对象,但有以下错误:

self.object

以下是代码段:

 MyModelUpdateView object has no attribute 'object'

即使正确更新了get_success_url(),get_object()似乎也有两个相互矛盾的要求: 1)能够使用现有的“example-model”slug字段获取正确的对象; 2)能够在未重定向现有URL之前使用新生成的“example-model-changed”获取更新的对象。

3 个答案:

答案 0 :(得分:4)

您不必在self.get_object方法中使用get_success_url。保存表单时form_valid()设置self.object,因此您可以使用self.object.slug获取新网址。

def get_success_url(self):
    view_name = 'update_mymodel'
    # No need for reverse_lazy here, because it's called inside the method
    return reverse(view_name, kwargs={'model_name_slug': self.object.slug})

答案 1 :(得分:0)

没有太多使用基于类的视图,但是..

Main.hs:198:3:
    Couldn't match expected type ‘(Maybe Text -> f (Maybe Text))
                                  -> Instance -> f Instance’
                with actual type ‘Maybe Text’
    Relevant bindings include
      instPublicIP' :: (Maybe Text -> f (Maybe Text))
                       -> Instance -> f Instance
        (bound at app/Main.hs:197:1)
    In the expression:
      insNetworkInterfaces
      ^? ix 0 . iniAssociation . _Just . iniaPublicIP . _Just
    In an equation for ‘instPublicIP'’:
        instPublicIP'
          = insNetworkInterfaces
            ^? ix 0 . iniAssociation . _Just . iniaPublicIP . _Just

Main.hs:198:27:
    Couldn't match type ‘InstanceNetworkInterface’
                   with ‘Instance -> f0 Instance’
    Expected type: (InstanceNetworkInterface
                    -> Const (Data.Monoid.First Text) InstanceNetworkInterface)
                   -> (([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
                       -> Instance -> f0 Instance)
                   -> Const
                        (Data.Monoid.First Text)
                        (([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
                         -> Instance -> f0 Instance)
      Actual type: (IxValue
                      (([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
                       -> Instance -> f0 Instance)
                    -> Const
                         (Data.Monoid.First Text)
                         (IxValue
                            (([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
                             -> Instance -> f0 Instance)))
                   -> (([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
                       -> Instance -> f0 Instance)
                   -> Const
                        (Data.Monoid.First Text)
                        (([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
                         -> Instance -> f0 Instance)
    In the first argument of ‘(.)’, namely ‘ix 0’
    In the second argument of ‘(^?)’, namely
      ‘ix 0 . iniAssociation . _Just . iniaPublicIP . _Just’

如果get_success_url应该返回你要将用户重定向到的url,那么你不应该使用更新的slug而不是旧的slug吗?

def get_success_url(self): view_name = 'update_mymodel' return reverse_lazy(view_name, kwargs={'model_name_slug': self.kwargs.get('model_name_slug','')})

我想这是正确完成的。

  

1)能够使用现有的“example-model”获取正确的对象   slu field field;

您希望根据更新的重定向模型获取更新的网址。你在哪里创建更新的网址? get_success_url?那我不明白为什么self.get_object().model_name_slug不起作用?

  

2)能够使用新生成的方式获取更新的对象   在未重定向现有URL之前,“example-model-changed”。

答案 2 :(得分:0)

感谢Alasdair和eugene的快速帮助。

与此同时,我已经想出了另一种方法!也就是说,在URL中使用两个参数,例如和。

在stackoverflow URL中查找。你有没有发现类似的东西?是的,就是这样。我想是33115530是pk。其余的是自动生成的slug字段!

然后,在get_object中,将通过get_success_url获取新更新的对象,并且get_success_url可以使用对象中更新的新slug字段。

相关问题