Django 测试重定向以使用下一个参数登录

Django Test Redirect to login with next parameter

我正在尝试测试非登录用户重定向到登录 url。有没有办法将 get 参数(例如 'next')传递给 reverse_lazy?我正在使用基于 class 的视图。

class SecretIndexViewTest(TestCase):
    url = reverse_lazy('secret_index')
    login_url = reverse_lazy('login')
    client = Client()

    def test_http_status_code_302(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 302)
        self.assertRedirects(response, self.login_url,)

以上失败

Response redirected to '/login?next=/secret', expected '/login'Expected '/login?next=/secret' to equal '/login'.

尝试过

class SecretIndexViewTest(TestCase):
    url = reverse_lazy('secret_index')
    login_url = reverse_lazy('login', kwargs={'next': url)
    client = Client()

    def test_http_status_code_302(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 302)
        self.assertRedirects(response, self.login_url,)

结果是没有反向匹配

django.urls.exceptions.NoReverseMatch: Reverse for 'login' with keyword arguments '{'next': '/secret'}' not found. 1 pattern(s) tried: ['/login$']

将 kwargs 更改为 args 将导致相同的错误。

我知道我可以用下面的方法处理它,但我正在寻找更好的解决方案。

self.assertRedirects(response, self.login_url + f"?next={self.url}",)

login_url = reverse_lazy('login') + f"?next={url}"

由于 mentioned in the Django documentationreverse(和 reverse_lazy)不包含 GET 参数,因此您的字符串格式化示例是最简单的解决方案之一。

The URLconf searches against the requested URL, as a normal Python string. This does not include GET or POST parameters, or the domain name.

For example, in a request to https://www.example.com/myapp/, the URLconf will look for myapp/.

In a request to https://www.example.com/myapp/?page=3, the URLconf will look for myapp/.

The URLconf doesn’t look at the request method. In other words, all request methods – POST, GET, HEAD, etc. – will be routed to the same function for the same URL.

如果您担心对查询字符串进行编码,可以使用 urllib.parse module 中的工具(例如 urllib.parse.urlencode)在构建完整 URL 之前通过串联来自 reverse 的路径和编码的查询参数。

对于简单的情况,例如您确切知道传入的数据是什么的测试,我倾向于坚持使用简单的字符串格式。