0

I am trying to create a form that gets JSON inputs through a textarea(textarea1) and then processes it and prints the response back in another text area(textarea2) while still showing the original json input from textarea1. I have the code that takes the input computes the result and puts the value back. But the values are not shown in the new form. My server does not use any models.

Here is the code from views.py

from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect

import simplejson
import read_json
from .forms import JsonTestForm

import sys
import traceback

def index(request):
    form = JsonTestForm()
    return render(request, 'ellora/index.html', {'form': form})

def get_json_text(request):
    print "Enter method get_json_text"
    if request.method == 'POST':
        print "request method is post"
        form = JsonTestForm(request.POST)
        if form.is_valid():
            print "form is valid"
            #call the read_json.py and pass the json script in the appropriate format
            # capture the result of it in some way and then redirect it to the results page
        try:
            data_string=form.cleaned_data['jsonText']
            data = simplejson.loads(data_string)
            #do some processing of the data here
            f = open("temp/test7777777.py", "r")
            form.cleaned_data['pythonScript'] = f.read()
            return render(request, "ellora/index.html", {"form": form})
        except Exception as e:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
            print ''.join('! ' + line for line in lines)            
            mystr = ''.join('! ' + line for line in lines)
            form.cleaned_data['pythonScript'] = mystr
            print "cleanded_data=", form.cleaned_data['pythonScript']
            return render(request, "ellora/index.html", {"form": form})
    else:
        print "request type was not POST"

code from my forms.py

from django import forms

class JsonTestForm(forms.Form):
    jsonText = forms.CharField(label="", widget=forms.Textarea(attrs={"class": "txtarea", "placeholder": "Enter your json script here"}), initial="[]")
    pythonScript = forms.CharField(label="", widget=forms.Textarea(attrs={ "class": "txtarea", "readonly": "readonly", "rows": "1", "cols": ""}), initial="python script here")
    testLog = forms.CharField(label="", widget=forms.Textarea(attrs={ "class": "txtarea", "readonly": "readonly", "rows": "1", "cols": ""}), initial="logs here")

Thanks for your help

Gatothgaj
  • 1,633
  • 2
  • 16
  • 27

1 Answers1

1

You need to use AJAX. This should give you an idea of how to go about this. AngularJS would also allow you to easily do this, depending on the logic required.

Community
  • 1
  • 1
MD Islam
  • 137
  • 8
  • Hi Thanks for your reply, I tried that and now I get 403 forbidden error. I read that it is because of csrf token. I do not know where to add the csrf token in the script. I appreciate your help – Gatothgaj Jul 28 '15 at 02:23
  • I forgot to tell you that. Make sure you have the CSRF token template tag in your HTML, and send this as data with your AJAX call (use jquery $.ajax): `{csrfmiddlewaretoken: document.getElementsByName('csrfmiddlewaretoken')[0].value}` – MD Islam Jul 28 '15 at 14:14
  • Hi, I am not very clear.. I tried that now the page comes back but does not execute success or failure inside ajax. An example with my view.py would be really helpful. – Gatothgaj Jul 28 '15 at 16:20
  • Instead of returning render(), return a HttpResponse(). It should look something like this, `return HttpResponse(json.dumps(json_object), content_type='application/json')` You can return nothing and it will hit failure. – MD Islam Jul 28 '15 at 16:24