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

uoifb46i  于 2023-05-30  发布在  Go
关注(0)|答案(1)|浏览(202)

我有一个输入字段<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
2ul0zpep

2ul0zpep1#

重写类LoginView上的方法get_context_data

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

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

相关问题