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