4

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?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
gatlanticus
  • 1,158
  • 2
  • 14
  • 28

2 Answers2

5

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/')
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks. Although it may be bad practice, using c.get with the absolute url works, at least through `./manage.py shell`. However, your [link to the docs](https://docs.djangoproject.com/en/1.7/topics/testing/tools/) helped me find the main problem... "The test client is not capable of retrieving Web pages that are not powered by your Django project. If you need to retrieve other Web pages, use a Python standard library module such as urllib." – gatlanticus Apr 03 '15 at 02:33
  • Thanks! It's impotent use `/` before api path. – Serg Smyk Dec 19 '19 at 19:36
1

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)
Community
  • 1
  • 1
agconti
  • 17,780
  • 15
  • 80
  • 114