我正在尝试创建一个简单的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>
和我的观点.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
。此时页面呈现正确,但按下按钮后什么都没有发生,外部站点也没有接收到任何数据。
您的代码中有几个错误需要首先修复
...
def my_view(requests):
if requests.method == "POST":
# Here is 1, use POST instead
# text_thing= request.GET.get('text_thing')
# number_thing= request.GET.get('number_thing')
# https://docs.djangoproject.com/en/3.1/ref/request-response/#django.http.HttpRequest.POST
text_thing = request.POST.get('text_thing', None)
number_thing = request.POST.get('number_thing', None)
# Here is 2, you are not creating a dict this way,
# You are creating a set, put the variables with the keys.
post_data = {'text_thing': text_thing, 'number_thing': number_thing}
....
如果您的站点没有接收到任何数据,请检查其他站点的响应,以确保您调用的是正确的API。
我希望这能帮助
您的代码中有许多拼写错误。你把requests
和request
搞混了。您正在导入请求模块并将其传递给my_view()
,这是不正确的。
my_view()
是一个视图函数,它接受request
对象,而不是requests
库。
而且您应该使用request。POST以获取有效负载。要求GET返回查询参数,而不是有效负载。
这是更新后的代码
from django.shortcuts import render, redirect
import requests
def my_view(request):
if request.method == "POST":
text_thing= request.POST.get('text_thing')
number_thing= request.POST.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(request, 'my_view.html', {})
感谢@Radwan Abu Odeh和@Underoos,我成功地让它工作了。非常感谢你的帮助。
工作示例:.html代码
<form method="POST">
<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
def my_view(request):
if request.method == "POST":
text_thing= request.POST.get('text_thing', None)
number_thing= request.POST.get('number_thing', None)
post_data = {'text_thing': text_thing, 'number_thing': number_thing}
if text_thing is not None:
response = requests.post('example.com', data=post_data)
return redirect('home')
else:
return redirect('home')
else:
return render(request, 'my_view.html', {})