django中redirect_field_value的含义是什么?

时间:2018-04-15 07:46:08

标签: django django-templates django-views

此代码的含义是什么?

{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}

1 个答案:

答案 0 :(得分:0)

当我们使用django-allauth库时,可以使用上面的html片段从django应用程序Example in SO中注销,在表单中使用redirect_field_value将其发布到URL account_logout或account / logout及其关联视图负责行动

注意:默认情况下,redirect_field_name ='next'传递给函数get_next_redirect_url以获取重定向URL。

class LogoutView(TemplateResponseMixin, View):

    template_name = "account/logout." + app_settings.TEMPLATE_EXTENSION
    redirect_field_name = "next"

    def post(self, *args, **kwargs):
        #self.get_redirect_url method is called by passing the redirect_field_name='next' 
        #which is used further to logout the authenticated user 
        url = self.**get_redirect_url()**
        if self.request.user.is_authenticated:
            self.logout()
        response = redirect(url)
        return _ajax_response(self.request, response)

    def logout(self):
        adapter = get_adapter(self.request)
        adapter.add_message(
        self.request,
        messages.SUCCESS,
        'account/messages/logged_out.txt')
    adapter.logout(self.request)

    def get_redirect_url(self):
        return (
            get_next_redirect_url(
                self.request,
                **self.redirect_field_name**) or get_adapter(
                    self.request).get_logout_redirect_url(
                        self.request))

正如我所看到的,每个视图(SignUp,Login,Logout)都定义了默认的rediect_field_name,因此执行注销操作时,删除上面的标记仍将起作用。

<form method="post" action="{% url 'account_logout' %}">
{% csrf_token %}
<!-- {% if redirect_field_value %}
<input type="hidden" name="{{redirect_field_name}}" value="{{redirect_field_value}}">
{% endif %} -->
<button type="submit" class="nav-link btn btn-sm sgp">SignOut</button>
</form>