我正在尝试测试非登录用户重定向到登录 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/