2015-04-03 40 views
2

我能夠通過我的Web瀏覽器(http://127.0.0.1:8983/solr)到達本地地址以查看Solr管理員(搜索webapp)。django測試客戶端獲取404,但瀏覽器工程

然而,通過Django的(1.7)測試客戶端,我得到:

>>> from django.test import Client 
>>> c = Client() 
>>> response = c.get('http://127.0.0.1:8983/solr') 
>>> response.status_code 
404 

爲什麼不能Django的連接到同一個地址(ES)作爲我的瀏覽器?

回答

3

你應該提供相對URLget()

c.get("/solr/") 

這是在Testing Tools頁記載:

當檢索頁面,記得指定URL的路徑,而不是 整個域名爲。例如,這個正確的是:

c.get('/login/') 

這是不正確的:

c.get('http://www.example.com/login/') 
+0

謝謝。雖然這可能是一種不好的做法,但至少通過'./manage.py shell'來使用絕對URL的c.get。但是,[鏈接到文檔](https://docs.djangoproject.com/en/1.7/topics/testing/tools/)幫助我發現了主要問題......「測試客戶端無法檢索Web如果您需要檢索其他網頁,請使用Python標準庫模塊,例如urllib。「 – ZG101 2015-04-03 02:33:16

+0

@ ZG101正確點,感謝分享。 – alecxe 2015-04-03 02:36:49

1

添加到@ alecxe的answer,你應該指定相對URL;它的最佳做法是使用reverse()獲取網址:

from django.core.urlresolvers import reverse 
from django.test import Client 
c = Client() 

# assuming you've named the route 'solar' 
url = reverse('solar') 
c.get(url) 
相關問題