I am a total newbie to Python and Django and am playing around with passing data, via http post, to a simple web app. I went through the Django tutorial and have a decent understanding of what was being taught but I don't quite have the urls.py regex filtering down. I've successfully used urlopen() as an http get. Not so successful with using it to POST. It appears as though my app urls.py (not the project urls.py) doesn't allow the POST to get to my app views.py. Utlimately I want to grab the data that I'm passive via POST and return it in my response. Here's what I have:
My project urls.py:
urlpatterns = patterns('',
url(r'^timeapp/', include('timeapp.urls')),
url(r'^datasink/', include('datasink.urls')),
url(r'^admin/', include(admin.site.urls)),
)
datasink/ urls.py:
urlpatterns = patterns('',
(r'^$', views.index),
)
datasink/ views.py
def index(request):
if request.method == 'GET':
return HttpResponse("This was a GET")
else:
return HttpResponse("This was a not a GET ... POST?")
client side
from urllib2 import Request, urlopen, URLError, build_opener, HTTPHandler
import urllib
url = "http://127.0.0.1:8080/datasink/"
data = {'name':'joe'}
encodedData = urllib.urlencode(data)
req = Request(url)
req.add_data(encodedData)
try:
response = urlopen(req)
print "We received the following response from our httpREQ: " +response.read()
print "The response.geturl(): " +response.geturl()
except URLError as e:
if hasattr(e, 'reason'):
print 'We failed to reach a server.'
print 'Reason: ' +str(e.reason) +str(e.code)
elif hasattr(e, 'code'):
print 'The server couldn\'t fulfill the request.'
print 'Error code: ' + str(e.code)
else:
pass
Error on server side:
Exception AttributeError: AttributeError("'_DummyThread' object has no attribute 'Thread_block'",) in ignored [02/Apr/2013 05:15:57] "POST /datasink HTTP/1.1" 500 54048
Message on client:
We failed to reach a server. Reason: INTERNAL SERVER ERROR
Any guidance would be much appreciated. I've been pulling my hair out trying various regex expressions ... no luck.