Open URL from Django

1,172 views
Skip to first unread message

Bojan Mihelac

unread,
Sep 7, 2008, 5:02:55 AM9/7/08
to Django users
Hi all,

I am trying to load page that is on the same server but Django just
hangs. If connection on another sever is opened it works as expected.
Is this because the local server does not support more then 1 request
at a time, or some other thing?

This is minimized code:

<pre>
#views.py
from django.http import HttpResponse
import urllib2

def test_open_url(request):
f = urllib2.urlopen(urllib2.Request(url="http://localhost:8000/
test_page"))
html = "<html><body>Loaded page: %s.</body></html>" % f.read()
return HttpResponse(html)
</pre>

Anyone knows why?

thanks,
Bojan

Bojan Mihelac

unread,
Sep 7, 2008, 6:40:50 AM9/7/08
to Django users
I have try similiar code under both Django dev server and Apache/
mod_python. Apache did load the result, while Django server did not.

Is this maybe threading issue?

here is samplecode:

#views.py
from django.http import HttpResponse
from django.core import urlresolvers
import urllib2

def test_open_url(request):
host = request.get_host()
test_url = "http://%s/%s" % (host,
urlresolvers.reverse("test_page"))
f = urllib2.urlopen(urllib2.Request(url=test_url))
html = "<html><body>Loaded page: %s.</body></html>" % f.read()
return HttpResponse(html)

def test_page(request):
return HttpResponse("test")

#urls.py
from django.conf.urls.defaults import *

import views

urlpatterns = patterns('',
(r'^$', views.test_open_url),
url(r'^test_page$', views.test_page, name = "test_page")
)

Eric Abrahamsen

unread,
Sep 7, 2008, 8:02:37 AM9/7/08
to django...@googlegroups.com

On Sep 7, 2008, at 6:40 PM, Bojan Mihelac wrote:

>
> I have try similiar code under both Django dev server and Apache/
> mod_python. Apache did load the result, while Django server did not.
>
> Is this maybe threading issue?

Nope, that's the way the Django development server works – it's single
threaded, and only serves one request at a time. If you really need to
test your view, you can start another instance on a different port.
But from looking at your code, my guess is that you're running
unittests on your site, in which case you can use the TestClient: http://docs.djangoproject.com/en/dev/topics/testing/#module-django.test.client

Yours,
Eric

Bojan Mihelac

unread,
Sep 7, 2008, 8:47:07 AM9/7/08
to Django users
Eric, thanks for answer, this make sense and I am aware of
TestClient.

Was not sure if this is an error...

Bojan

On Sep 7, 2:02 pm, Eric Abrahamsen <gir...@gmail.com> wrote:
> On Sep 7, 2008, at 6:40 PM, Bojan Mihelac wrote:
>
>
>
> > I have try similiar code under both Django dev server and Apache/
> > mod_python. Apache did load the result, while Django server did not.
>
> > Is this maybe threading issue?
>
> Nope, that's the way the Django development server works – it's single  
> threaded, and only serves one request at a time. If you really need to  
> test your view, you can start another instance on a different port.  
> But from looking at your code, my guess is that you're running  
> unittests on your site, in which case you can use the TestClient:http://docs.djangoproject.com/en/dev/topics/testing/#module-django.te...
Reply all
Reply to author
Forward
0 new messages