我正在尝试准备一个简单的UI。在单击提交按钮的下拉列表中,用户将从下拉列表中选择roboid
,他应使用其他API URL获取机器人详细信息。如果我硬编码roboid
我会得到结果。但我怀疑的是如何将值从下拉传递到URL。以及如何在同一窗口中以表格形式显示json。
这是我的代码:
Views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
import ast
from django.http import HttpResponse
import requests
from .form import NameForm
def get_name(request):
# if this is a POST request we need to process the form data
title = 'welcome'
k = {}
form = NameForm(request.POST or None)
context = {
"form": form, "title": title, "roboid": 2}
if form.is_valid():
robotid = request.POST.get('roboid', '')
print robotid
resp = requests.get('https://r0p1i0hwdh.execute-api.us-west-2.amazonaws.com/testhp/?roboid=%s' % robotid)
l = ast.literal_eval(resp.content)
di = ast.literal_eval("".join(map(str, l)))
for k, v in di.iteritems():
items = "{:<25} {:<50}".format(k, v)
context = {
"robotid": robotid}
return render(request, "name.html", context)
name.html a级:
< !DOCTYPE html >
< html
< head >
< / head >
< body bgcolor = "#f5f5dc" >
< center >
<h1 > {{title}} < / h1 >
< form
method = "get"
action = "" >
{ % csrf_token %}
{{form.as_p}}
< script >
var
z = "https://bvdruneuqc.execute-api.us-west-2.amazonaws.com/test/my-resource1?roboid=5"
< / script >
< input
type = "submit"
value = "Submit"
style = "float: none "
onclick = "window.open(z)" / >
< input
type = "RESET"
value = "cancel" >
{{items}}
< / form >< / center >
< / body >< / html >
form.py
from django import forms
class NameForm(forms.Form):
roboid = forms.ChoiceField(label='RobotID',choices=[(x, x) for x in range(1, 10)],required='TRUE',)
我知道在代码中还有很多要修改的内容。 但请帮助我们,这对我来说很重要。
答案 0 :(得分:0)
实际上你的问题并不清楚。所以,我正在回答你从查询中理解的内容。
将表单方法从获取更改为 name.html 中的发布,以便可以向您的视图和表单发送请求使用request.POST。
添加jquery以获取所选的roboid并追加到url中。 您也可以在API中附加所选的roboid,这也就是下面的注释。您可以根据自己的需要使用。
<强> name.html 强>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body bgcolor="#f5f5dc">
<center>
<h1> {{title}} </h1>
<form method="post" action="">
{% csrf_token %}
{{form.as_p}}
<script>
var z = "https://bvdruneuqc.execute-api.us-west-2.amazonaws.com/test/my-resource1";
$(document).on('click', 'input[type="submit"]', function(e){
e.preventDefault();
var roboid = $("#id_roboid option:selected").val();
$('form').attr('action', "?roboid=" + roboid).submit();
// var new_z = z + "?roboid=" + roboid;
// window.open(new_z);
});
</script>
<input type="submit" value="Submit" style="float:none" />
<input type="RESET" value="cancel"> {{items}}
</form>
</center>
</body>
</html>
在view.py中,因为roboid是通过附加在URL中发送的,因此可以从请求中获取.GET
<强> views.py 强>
def get_name(request):
# if this is a POST request we need to process the form data
title = 'welcome'
k = {}
form = NameForm(request.POST or None)
context = {
"form": form, "title": title, "roboid": 2}
if form.is_valid():
robotid = request.GET.get('roboid', '')
print ('robotid', robotid)
resp = requests.get('https://r0p1i0hwdh.execute-api.us-west-2.amazonaws.com/testhp/?roboid=%s' % robotid)
l = ast.literal_eval(resp.content)
di = ast.literal_eval("".join(map(str, l)))
for k, v in di.iteritems():
items = "{:<25} {:<50}".format(k, v)
context = {
"robotid": robotid}
return render(request, "name.html", context)
以及如何在同一窗口中以表格形式显示json。 为此,您可以提供数据,即您希望在窗口中显示的json格式的简短示例以及您正在使用的变量。因为现在项 valriable没有添加到上下文中的任何位置,因此无法在name.html中访问