:
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
答案 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']