1

I am learning Django so I've set up a very simple form/view/url example

  • Django Version 1.5.1
  • MATLAB Version R2012A

forms.py

from django import forms
import json

class json_input(forms.Form):

    jsonfield = forms.CharField(max_length=1024)

    def clean_jsonfield(self):
        jdata = self.cleaned_data['jsonfield']
        try:
            json_data = json.loads(jdata)
        except:
            raise forms.ValidationError("Invalid data in jsonfield")
        return jdata

views.py

from django.http import HttpResponse
from rds.forms import json_input

def testpost(request):

    if request.method == 'GET':

        form = json_input(request.GET)
        if form.is_valid():
            return HttpResponse('Were Good Get',mimetype='text/plain')

    elif request.method == 'POST':

        form = json_input(request.POST)
        if form.is_valid():
            return HttpResponse('Were Good Post',mimetype='text/plain')

    else:
        return HttpResponse('Not GET or POST.',mimetype='text/plain')

This view is mapped to the url in urls.py

url(r'^test2$','rds.views.testpost'),

So when I jump into the python manage.py shell on the local machine django is on I can issue the following commands and get the expected responses:

>>> from django.test.client import Client
>>> c = Client()
>>> r = c.post('/test2',{'jsonfield': '{"value":100}'})
>>> print r
Content-Type: text/plain

Were Good Post
>>> r = c.get('/test2',{'jsonfield': '{"value":100}'})
>>> print r
Content-Type: text/plain

Were Good Get

However when I jump into MATLAB on an external machine and issue the following commands. (Note doing this from MATLAB is a project requirement)

json = '{"value":100}';

% GET METHOD FOR JSON FORM
[gresponse,gstatus]=urlread('http://aq-318ni07.home.ku.edu/django/test2','Get',{'jsonfield' json});

% POST METHOD FOR JSON FORM
[presponse,pstatus]=urlread('http://aq-318ni07.home.ku.edu/django/test2','Post',{'jsonfield' json});

>> gresponse
    gresponse =
    Were Good Get
>> presponse
    presponse =
         ''

I have searched around for a solution and really cant find anything. I've hit on it potentially being an issue with the CSRF (which I am still figuring out). Any hints or thoughts would be much appreciated.

Thank you.

EDIT:

Django is exposed through Apache, here is the configuration.

################################################
# Django WSGI Config
################################################

WSGIScriptAlias /django /var/django/cdp/cdp/wsgi.py
WSGIPythonPath /var/django/cdp

<Directory /var/django/cdp/cdp>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

################################################
  • A note, this form never needs to be displayed (no template). The only thing needed is a response which will in the end either be a text status, result of a query (as JSON), or data (as JSON). –  May 22 '13 at 15:26

1 Answers1

0

how are you exposing your django app for MATLAB? The very first thing is to check your access logs, is your server even getting a request? If so anything in its error logs?

Are you using the built in developkment server? python manage.py runserver 0.0.0.0:8000 If so make sure that you can accept requests on that port

If you are serving it through another server i believe you have to whitelist the IP that you are making request from MATLAB, by adding it to ALLOWED_HOSTS

dm03514
  • 54,664
  • 18
  • 108
  • 145
  • Sorry for omitting this information. Django is routed through Apache using WSGI. This will eventually be a public project so adding individual IP's wouldnt work. I'll add an edit with my WSGI/Apache config. –  May 22 '13 at 15:28
  • @kpurdon is the request showing up in the apache access logs? – dm03514 May 22 '13 at 15:29
  • yes it is. "POST /django/test2 HTTP/1.1" 403 2282 "-" "MATLAB R2012a" –  May 22 '13 at 15:33
  • It really seems like a CSRF issue. However I cannot find any information on how to implement this for my case, where no template is rendered. Any thoughts? –  May 22 '13 at 15:57
  • @kpurdon did you use the `@csrf_exempt` decorator to verify that it is a csrf issue? What does your error log say? 403 seems like it would be a csrf issue – dm03514 May 22 '13 at 15:58
  • With '@csrf_exempt' everything works fine confirming it's a csrf issue. How can I implement csrf protection in my example? –  May 22 '13 at 16:23
  • @kpurdon, Do you really need csrf protection, if you are making requests from MATLAB ( i am not familiar with MATLAB, and have never used it)? If you want to authenticate your clients, you can do so easily using a framework like django REST framework, which supports a number of authentication schemese out of the box, If anyone can make a reqeust to your url then i don't think you really need csrf – dm03514 May 22 '13 at 16:27
  • 1
    Thanks, I think i'm on the right path now. Looks like the django REST framework will resolve some of my concerns. –  May 22 '13 at 16:34