我正在尝试创建一个通过textarea(textarea1)获取JSON输入的表单,然后处理它并将响应打印回另一个文本区域(textarea2),同时仍然显示textarea1的原始json输入。我有代码,它接受输入计算结果并将值返回。但这些值未以新形式显示。我的服务器不使用任何型号。
以下是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"
来自我的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")
感谢您的帮助