从“仅发布后”视图重定向到网址

时间:2016-06-16 13:14:09

标签: django url redirect

我有一个DetailView和FormView组合的以下设置:

class EventBookView(LoginRequiredMixin, View):
    def get(self, request, *args, **kwargs):
        view = EventBookBaseView.as_view()
        return view(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        view = EventBookFormView.as_view()
        return view(request, *args, **kwargs)

class EventBookBaseView(DetailView):
    template_name = "event_book.html"
    model= Event
    context_object_name = 'event'

class EventBookFormView(SingleObjectMixin, FormView):
    template_name = "event_book.html"
    form_class = PersonalInfoForm
    model = Event
    context_object_name = 'event'

    def post(self , request , *args , **kwargs):
        #do stuff
        ...
        return redirect('user_bookings')

不幸的是,重定向到名为'user_bookings'的网址无效。如何重定向到该网址?

谢谢!

2 个答案:

答案 0 :(得分:0)

您可以使用success_url,正确处理表单后,会重定向视图。

例如

class MyFormView(FormView):
    success_url = reverse_lazy('user_bookings')

编辑:更改为reverse_lazy

答案 1 :(得分:-1)

POST方法不应返回重定向。 如果您需要,请尝试使用HttpResponseRedirect。

像:

return HttpResponseRedirect('/user_bookings/')