python - 要将 Turbolinks 5 与 Django 一起使用,如何在使用 redirect() 时自动包含 Turbolinks-Location header ?

标签 python django redirect http-headers turbolinks

根据“跟随重定向”的 Turbolinks 5 文档 (https://github.com/turbolinks/turbolinks#following-redirects):

When you visit location /one and the server redirects you to location /two, you expect the browser’s address bar to display the redirected URL.

However, Turbolinks makes requests using XMLHttpRequest, which transparently follows redirects. There’s no way for Turbolinks to tell whether a request resulted in a redirect without additional cooperation from the server.

解决方案是:

send the Turbolinks-Location header in response to a visit that was redirected, and Turbolinks will replace the browser’s topmost history entry with the value you provide.

The Turbolinks Rails engine performs this optimization automatically for non-GET XHR requests that redirect with the redirect_to helper.

我对在我的 Django (1.11) 项目中使用 Turbolinks 非常感兴趣,我想知道是否有人可以指出正确的方向,告诉我如何创建一个新的 Django redirect() 函数或将现有的函数修改为始终包括重定向所需的 Turbolinks-Location header 以按预期运行。我绝对不想每次进行重定向时都手动设置此 header 。

在“表单提交后重定向”部分 ( https://github.com/turbolinks/turbolinks#redirecting-after-a-form-submission ) 中有一个类似的条目,如果您能帮助我理解如何实现,我将不胜感激:

If form submission results in a state change on the server that affects cached pages, consider clearing Turbolinks’ cache with Turbolinks.clearCache().

The Turbolinks Rails engine performs this optimization automatically for non-GET XHR requests that redirect with the redirect_to helper.

我确实看到 github 上有一个“Drop-in turbolinks implementation for Django”包,但这是从 turbolinks-classic fork 出来的,源代码没有提到 Turbolinks-Location header ,所以我确定这不是我想要的我在找。

最佳答案

通过指向该项目中的一段代码,我确实最终发现了如何准确地完成我正在尝试的事情 https://github.com/viewflow/django-material/blob/v2/material/middleware.py由 reddit 用户提供。

我将 TurbolinksMiddleware 类复制到我自己的应用程序目录下的 middleware.py 中,并将其列在我的 settings.py 中

# MIDDLEWARE CONFIGURATION
# --------------------------------------------------------------------------
MIDDLEWARE = [
    .
    .
    .
    'apps.middleware.TurbolinksMiddleware',
]

通过在我的 html 模板中包含 turbolinks.js 的常规安装步骤,一切似乎都正常工作。

这里是 TurbolinksMiddleware 类,以防它在上面的链接中不可用:

class TurbolinksMiddleware(object):
    """
    Send the `Turbolinks-Location` header in response to a visit that was redirected,
    and Turbolinks will replace the browser’s topmost history entry .
    """

    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)

        is_turbolinks = request.META.get('HTTP_TURBOLINKS_REFERRER')
        is_response_redirect = response.has_header('Location')

        if is_turbolinks:
            if is_response_redirect:
                location = response['Location']
                prev_location = request.session.pop('_turbolinks_redirect_to', None)
                if prev_location is not None:
                    # relative subsequent redirect
                    if location.startswith('.'):
                        location = prev_location.split('?')[0] + location
                request.session['_turbolinks_redirect_to'] = location
            else:
                if request.session.get('_turbolinks_redirect_to'):
                    location = request.session.pop('_turbolinks_redirect_to')
                    response['Turbolinks-Location'] = location
        return response

关于python - 要将 Turbolinks 5 与 Django 一起使用,如何在使用 redirect() 时自动包含 Turbolinks-Location header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47240766/

相关文章:

python - 如何导入带有 "occasional"引号的 csv(R 和/或 Pandas)?

django - 如何使用 Django ORM 删除 id 最小的行?

java - JSF 在页面加载时重定向

jsp - 如何将Servlet重定向到给定的URL

python - 一维数据中的步进检测

python - 访问嵌套小部件的方法

python - 为 PyLint 输出着色?

django - 您可以制作一个返回查询集的自定义模板标签吗?如果是,如何? - Django

python - 在 Python (Django) 中解析文本

php - CakePHP 根据用户名在登录时重定向