重定向到Django 1.5中的下一个参数

时间:2013-04-27 13:27:50

标签: python django redirect next loginview

我有一个输入字段<input type="hidden" name="next" value="{{ next }}" />,但它不会用GET参数的值替换{{ next }}。我找不到如何替换这个值。

我的观点

class LoginView(FormView):
    form_class = AuthenticationForm
    redirect_field_name = REDIRECT_FIELD_NAME
    template_name = 'user/login.html'

    @method_decorator(csrf_protect)
    @method_decorator(never_cache)
    def dispatch(self, *args, **kwargs):
        return super(LoginView, self).dispatch(*args, **kwargs)

    def form_valid(self, form):
        login(self.request, form.get_user())
            return super(LoginView, self).form_valid(form)

    def get_success_url(self):
        if self.success_url:
            redirect_to = self.success_url
        else:
            redirect_to = self.request.REQUEST.get(self.redirect_field_name, '')

        if not redirect_to:
            redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)

        return redirect_to

1 个答案:

答案 0 :(得分:1)

在类get_context_data上覆盖方法LoginView

def get_context_data(self, **kwargs):
    context = super(LoginView, self).get_context_data(**kwargs)
    context['next'] = self.request.REQUEST.get('next')
    return context

这假定GET参数将具有下一个url,其中用户应在param next中重定向。因此,请求应为localhost.com/login?next=/accounts/

格式