0

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.

  • I just built a brand new project just to see if that would change things but I have the same issue. I am 99% confident that the project urls.py is filtering my POST but I don't know enough about regex to know why. – user2234592 Apr 03 '13 at 02:35
  • Just figured it out. Default Django projects turn on CSRF. In my project settings.py file I commented out the django.middleware.csrf.CsrfViewMiddleware line and the POST worked. Now it's time to uncomment that and create a CSRF token. – user2234592 Apr 03 '13 at 04:38

1 Answers1

0

There is a difference in the datasink and datasink/ path.

In this case you have defined datasink/ but nothing is handling datasink.

Try changing

url = "http://127.0.0.1:8080/datasink"

to

url = "http://127.0.0.1:8080/datasink/"

in your client to see if this solves the problem.

user2172816
  • 1,200
  • 8
  • 11
  • Good catch. Now I'm getting a 403 error with POST and GET still works fine. – user2234592 Apr 02 '13 at 15:14
  • This problem is killing me ... I'm still of the mindset that I'm not redirecting correctly. GET works fine, POST gives me 403 and doesn't hit the app urls.py. – user2234592 Apr 03 '13 at 01:13
  • @user2234592 Maybe you can try the solution [here](http://stackoverflow.com/questions/10663446/post-method-always-return-403-forbidden) – user2172816 Apr 12 '13 at 12:12