我有一個視圖,當用戶在未登錄時嘗試訪問/amos
時,應該將其重定向到/accounts/login/?next=/amos/
。爲了測試此操作,我使用以下測試:Django的測試客戶端返回錯誤編碼的URL
import urllib
# I actually define this function elsewhere, I placed it here for brevity's sake
def url_with_querystring(path, **kwargs):
return path + '?' + urllib.urlencode(kwargs)
def setUp(self):
self.client = Client()
def test_call_view_denies_anonymous(self):
target_url = reverse('amos_index')
redirect_url = url_with_querystring(reverse('login'), next='/amos/')
response = self.client.get(target_url, follow=True)
self.assertRedirects(response, redirect_url)
response = self.client.post(target_url, follow=True)
self.assertRedirects(response, redirect_url)
該錯誤與self.assertRedirects(response, redirect_url)
發生。具體地講,它提出了這樣的錯誤:
AssertionError: Response redirected to '/accounts/login/?next=/amos/', expected '/accounts/login/?next=%2Famos%2F'
顯然,錯誤來自/
不等於%2F
。 response
返回的重定向URL似乎只是一個原始字符串,而自定義函數返回一個正確的URL編碼的字符串。在這種情況下我該怎麼辦?我應該只是沒有網址編碼它或這是一個問題django.test.client
?
編輯:我讀過以前在DjangoProject網站上提出的bug。根據我的理解,這似乎是有意的行爲?也就是說,在Django的內部系統中,URL不會被編碼。想從更有經驗的人那裏得到一些見解,謝謝!
哦,很好的接收!感謝您爲我清理這個!我想我會在我的'url_with_querystring()'函數中模仿行爲(以便面向未來) –
只需重複一遍,編輯:'redirect_to_login'方法就能正確地對所有內容進行編碼*除正斜線用'!'測試)。你知道這是爲什麼嗎,@Alasdair? –
它不是URL編碼的客戶端 - 它是您在視圖中使用的裝飾器調用的'redirect_to_login'方法。 – Alasdair