Search code examples
djangotestingdjango-testingdjango-tests

django test client gets 404, but browser works


I am able to reach a local address through my web browser (http://127.0.0.1:8983/solr) to see the Solr Admin (search webapp).

However, through the Django (1.7) test client I get:

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

Why can't Django connect to the same address(es) as my browser?


Solution

  • You should provide relative URLs to get():

    c.get("/solr/") 
    

    This is documented at Testing Tools page:

    When retrieving pages, remember to specify the path of the URL, not the whole domain. For example, this is correct:

    c.get('/login/') 
    

    This is incorrect:

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