有没有办法从urlconf参数设置success_url?

时间:2015-07-09 00:01:58

标签: python django django-class-based-views

在urls.py中

url(r'^add/from?(?P<link>[a-zA-Z0-9/]+)$', PostAdd.as_view(), name='post_add'),
在views.py中

class PostAdd(CreateView):

model = Post
form_class = PostForm
template_name = 'post_add.html'
success_url = ????

def get_context_data(self, **kwargs):
    context = super(PostAdd, self).get_context_data(**kwargs)
    context['from'] = self.kwargs['link']
    return context

我可以将 link 传递给模板,但我需要设置 success_url = link

1 个答案:

答案 0 :(得分:2)

您应该覆盖视图上的get_success_url方法,而不是使用success_url属性。

class PostAdd(CreateView):

    def get_success_url(self):
        # Grab 'link' here and return it:
        return self.kwargs['link']