我正在构建一个网页,它接受用户的输入,并根据后端的信息返回一个表。我用的是网络框架金字塔。我目前的做法如下:
步骤3和步骤4是我遇到问题的地方。我不知道我需要做些什么才能把我的字典清单暴露给不伦不类的人。我已经阅读了“金字塔快速教程”,因此我对如何使用JSON呈现器来实现简单的AJAX有了一个想法,但我不知道如何将其实现到当前的情况中。
为了给出一个更好的想法,我的处理变形输入的函数如下(部分函数和模板是从官方Github repo提供的变形样本中改编而来的):
@view_config(route_name='query_log', renderer='templates/form.pt')
def query_log(request):
schema = Device().bind(request=request)
# Create a styled button with some extra Bootstrap 3 CSS classes
process_btn = deform.form.Button(name='process', title="Process")
form = deform.form.Form(schema, buttons=(process_btn,), use_ajax=True)
# User submitted this form
if request.method == "POST":
if 'process' in request.POST:
try:
appstruct = form.validate(request.POST.items())
# Save form data from appstruct
print("Enter ID:", appstruct["ID"])
print("Enter date:", appstruct["date"])
# This variable is what I want to feed to dynatable
results = parse_log(appstruct["ID"], appstruct["date"].strftime('%Y-%m-%d'))
json.dumps(results)
# Create ppoup
request.session.flash('Returning Results.')
# Redirect to the page shows after succesful form submission
return HTTPFound("/")
except deform.exception.ValidationFailure as e:
# Render a form version where errors are visible next to the fields,
# and the submitted values are posted back
rendered_form = e.render()
else:
# Render a form with initial default values
rendered_form = form.render()
return {
# This is just rendered HTML in a string
# and can be embedded in any template language
"rendered_form": rendered_form,
}如何通过JSON将这个结果变量传递给Dynatable,并将表呈现在表单下面?
发布于 2018-11-19 12:54:05
先试试婴儿舞步。
验证成功后,您的try块:
try:
appstruct = form.validate(request.POST.items())
# following depends on structure of data you need
results = dict("id" = appstruct["ID"],
"date" = appstruct["date"].strftime('%Y-%m-%d'))
data = json.dumps(results)
return dict(data=data)在目标模板中,接受data参数,呈现为趣味。
一旦准备就绪,就可以研究如何通过XHR请求传递data,这将取决于您对JavaScript库的选择。
https://stackoverflow.com/questions/53372792
复制相似问题