success_url in UpdateView, based on passed value

后端 未结 5 1063
我在风中等你
我在风中等你 2020-12-23 20:52

How can I set success_url based on a parameter?
I really want to go back to where I came from, not some static place. In pseudo code:

url(r         


        
5条回答
  •  时光说笑
    2020-12-23 21:06

    Define get_absolute_url(self) on your model. Example

    class Poll(models.Model):
        question = models.CharField(max_length=100)
        slug = models.SlugField(max_length=50)
        # etc ...
    
        def get_absolute_url(self):
            return reverse('poll', args=[self.slug])
    

    If your PollUpdateView(UpdateView) loads an instance of that model as object, it will by default look for a get_absolute_url() method to figure out where to redirect to after the POST. Then

    url(r'^polls/(?P\w+)/, UpdateView.as_view(
        model=Poll, template_name='generic_form_popup.html'),
    

    should do.

提交回复
热议问题