【发布时间】:2020-06-05 04:29:33
【问题描述】:
我对 Django 非常陌生,但几个小时以来我一直在尝试找出解决方案。我想将一个变量从 html 表单传递到后端,然后在那里使用该变量来发出 api 请求。我希望 API 结果显示在 html 文件上,并根据列表(在后端)进行检查,并获得 html 文件比较的结果。
我希望 onClick 在 HTML(这是一封电子邮件)上提交变量,这样一切都可以在没有多个按钮/表单的情况下工作。
我相信我有多个错误。
非常感谢任何帮助。
index.html
<body>
<form action="#" method="post">
{% csrf_token %}
<input type="text" class="form-control" id="emailvalue" placeholder="" value=" "name="emailvalue">
<input type="submit" value="Submit" onclick="location.href={% url 'script' %}"><hr>
The Associated IP addresses are:
{% for j in nonduplicate_list %}
<li>{{j}}</li>
{% endfor %}
<hr>
The unblocked IP addresses are:
{% for i in unblocked_ip %}
<li>{{i}}</li>
{% endfor %}
</form>
</body>
views.py
def get_ip(request):
if request.method == 'POST':
input_email = request.POST.get('emailvalue')
three_days_ago = datetime.datetime.now() - datetime.timedelta(days = 30)
time_in_format = three_days_ago.strftime('%Y-%m-%dT00:00:00.000Z')
security_domain = 'https://security.com/api/v1/logs?' + 'q=' + input_email + '&since=' + str(time_in_format)
print(security_domain)
r = requests.get(security_domain)
data = json.loads(r.content)
data1 = str(data)
ip_pattern = re.compile ('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
ip_result = re.findall(ip_pattern, data1)
nonduplicate_list = []
for i in ip_result:
if i not in nonduplicate_list:
nonduplicate_list.append(i)
print(nonduplicate_list)
threat_ip_list = ['1.1.1.1', '168.213.156.142', '2.2.2.2']
unblocked_ip = []
for i in threat_ip_list:
if i in nonduplicate_list:
unblocked_ip.append(i)
print(unblocked_ip)
return render(request, 'index.html', {'nonduplicate_list': nonduplicate_list, 'unblocked_ip': unblocked_ip})
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.button),
path('output/', views.get_ip, name = 'script')
]
【问题讨论】:
标签: html django python-3.x django-forms