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

标签 python django django-testing django-tests

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

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 documentation , reverse (和 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通过连接来自 reverse 的路径,在构建完整 URL 之前对您的查询进行编码和编码的查询参数。
对于简单的情况,例如您确切知道传入哪些数据的测试,我倾向于坚持使用简单的字符串格式。

关于python - Django 测试重定向以使用下一个参数登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66449900/

相关文章:

python - Django 测试 - 启动多个可以通信的测试服务器?

python - 使用 comtypes.client.GetModule() 为 dll 文件生成 python 包装器时出现断言错误

python - 使用 pysftp 模块重命名文件的权限被拒绝

javascript - 展开/折叠对象列表

Django 测试客户端忽略 Django 模板中的 "include"项

python - Django 中测试信号的基础知识

python - Holoviews 与 xarray 数据集 : How to scatter-plot two variables?

python - 如何使用两组正则表达式 url?

mysql - 在 Django 中执行批量 SQL 插入

django - 反向 django 自定义管理站点 URL