gatlanticus
gatlanticus

Reputation: 1226

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?

Upvotes: 7

Views: 2641

Answers (2)

agconti
agconti

Reputation: 18093

Adding to @alecxe's answer that you should be specifying relative urls; Its best practice to use reverse() to get the url:

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)

Upvotes: 1

alecxe
alecxe

Reputation: 473763

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/')

Upvotes: 5

Related Questions