要执行重定向,请执行return redirect([...])
。但是,只有在我另外执行def get_success_url(self):
的情况下,这才起作用。实际上,其中的url无关紧要,因为它使用的是我的def form_valid(self, form):
的重定向。您知道如何避免使用get_success_url
吗?
我还测试了将收益从form_valid
移到get_success_url
的情况。但是然后我收到一个错误,提示缺少返回。
class AdminRewardCreate(AdminPermissionRequiredMixin, SuccessMessageMixin, FormValidationMixin, AdminBaseRewardView, CreateView):
form_class = RewardForm
template_name = 'rewards/admin/create.html'
success_message = _("Reward has been successfully created.")
def form_valid(self, form):
instance = form.save(commit=False)
instance.event = self.request.event
# when the super method is called the instance
# is saved because it's a model form
super().form_valid(form)
return redirect(
'rewards:admin:index',
self.request.organizer.slug,
self.request.event.slug
)
def get_success_url(self):
return reverse(
'rewards:admin:index',
kwargs={
'organizer': self.request.organizer.slug,
'event': self.request.event.slug,
}
)
答案 0 :(得分:0)
您不需要从form_valid
方法调用重定向。只需从功能中返回超级调用。因此,我认为您可以像这样更新form_valid
:
def form_valid(self, form):
instance = form.save(commit=False)
instance.event = self.request.event
return super().form_valid(form) # you need to return the super call
def get_success_url(self):
return reverse(
'rewards:admin:index',
kwargs={
'organizer': self.request.organizer.slug,
'event': self.request.event.slug,
}
)
它应该可以解决问题,也请覆盖get_success_url
实现,因为它将重定向到该成功URL。