我正在尝试创建一个简单的 django 站点,该站点将使用该
requests.post
功能将信息发送到另一个站点(我假设这是一个正确的方法,但也许有更好的方法)。
到目前为止,我有一个使用引导程序创建的简单 .html 文件
<div class="form-group">
<label for="exampleFormControlInput1">text_thing</label>
<input type="text" class="form-control" placeholder="text_thing" name="text_thing">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">number_thing</label>
<input type="number" class="form-control" placeholder="number_thing" name="number_thing">
</div>
<button type="submit" class="btn btn-secondary">Send data</button>
</form>
和我的views.py
from django.shortcuts import render, redirect
import requests
def my_view(requests):
if requests.method == "POST":
text_thing= request.GET.get('text_thing')
number_thing= request.GET.get('number_thing')
post_data = {'text_thing', 'number_thing'}
if text_thing is not None:
response = requests.post('https://example.com', data=post_data)
content = reponse.content
return redirect('home')
else:
return redirect('home')
else:
return render(requests, 'my_view.html', {})
我希望该站点执行以下操作:呈现自身允许用户在两个字段中输入内容,并在按下按钮后重定向到另一个站点 - 在这种情况下'home'
,输入发送到example.com
. 此时页面正确呈现,但按下按钮后没有任何反应,外部站点也没有收到任何数据。